How do I select multiple fields in the same query?

Asked

Viewed 159 times

3

I created a dummy bank with the following structure:

Esquema Lógico

Below are also the numbered SQL Server scripts for database creation, if necessary.

SQL Fiddle

Now, what I want to do is make an appointment where they’re shown:

  1. The limit of an additional holder - present in the table Titres_additional
  2. The name of this client - present in the table Client
  3. The degree of kinship of this client - also in Titres_additional
  4. Who this holder depends on - also in the table Client, referenced in the table Bill
  • It is difficult to understand, write the structure of the tables and how you want the result. There are things there that do not seem to make sense. If you want you can even use http://www.sqlfiddle.com/ to assemble the whole structure, put data and we assemble the query for you.

  • @bigown edited the post, I had to reduce the scheme of this bank to post, but now it became much clearer - I believe.

  • I may be wrong because the modeling is a little confusing but it seems to me that you do not want a query but several, it does not seem that there is direct relationship at least between some of this information. It seems strange these relationships. And a hint, just put as number what really needs to be number. http://answall.com/q/47871/101 I remembered that I need to tweak the answer in another question of yours. They taught you to do something confusing there and it seems you liked the confusion :) Don’t zip, make a fiddle. http://www.sqlfiddle.com/#! 6/dd4e4

  • Yes of course, I will test and I tell you, I could not use SQL Fiddle, when I was going to run the script to insert the data it gave error - I checked the code and ran on MS SQL Server normally. Anyway, thanks for the reply and the tips, I will definitely take note :)

  • I have executed your scripts without changing a single comma :)

  • Oh yes now I saw, I just put the tables on the left side where you build the schema, and I tried to do the rest on the right side, and looking here at the way you did the scripts are all in the schema construction. Interesting...

Show 1 more comment

1 answer

2


If I have understood, it is more or less this (I hope I have not interpreted anything wrong):

SELECT Titulares_Adicionais.Limite, Titulares_Adicionais.Grau_Parentesco,
    Conta.Cod_Titular, Cliente.Nome
FROM Titulares_Adicionais
INNER JOIN Conta ON Titulares_Adicionais.Cod_Conta = Conta.Codigo
INNER JOIN Cliente ON Titulares_Adicionais.Cod_Cli = Cliente.Codigo
WHERE Titulares_Adicionais.Cod_Cli = 5;

I put in the Github for future reference.

Deep down the first INNER JOIN is unnecessary there, but I left if you want to sophisticate and want to get information from her, is easy. Note that no data is taken from the table Conta, so it is not necessary.

But the second is necessary because it takes the Nome down at the table Cliente.

The INNER JOIN or just JOIN is that lists the tables you need based on the defined columns.

  • Come on, you’re a badass, not that it really worked - http://www.sqlfiddle.com/#! 6/dd4e4/1/0 - I’m still coming to INNER JOIN, that’s why I didn’t know. I was actually told that only a SELECT gave. ; Anyway thank you so much :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.