6
I need to do SELECT
in two tables with relationship n
for n
, soon, I will have to make use of the intermediate table as well, but I don’t know how to do it. I am using PHP and Sqlserver database.
6
I need to do SELECT
in two tables with relationship n
for n
, soon, I will have to make use of the intermediate table as well, but I don’t know how to do it. I am using PHP and Sqlserver database.
6
You need to list all 3 tables using the JOIN command See a simple example.
SELECT * FROM tabelaA a
INNER JOIN tabelaRelacao r on a.id_tabelaA = r.id_tabelaA
INNER JOIN tabelaB b on a.id_tabelaB = r.id_tabelaB
Follows an image that graphically represents the Use of joins
.
More detailed than this could not :)
3
You need to make use of JOIN in your consultation.
SELECT
T.ID_Tabela1,
T.Descricao,
T3.Descricao
FROM Tabela1 T
INNER JOIN
Tabela2 T2 ON T2.ID_Tabela2 = T.ID_Tabela2
INNER JOIN
Tabela3 T3 ON T3.ID_Tabela3 = T2.ID_Tabela3
2
Use joins:
Inner Join: Registration required under A and B Left Join: Registration required on A, B may be null Right Join: Registration required on B, A may be null
SELECT
t1.nome, t1.id01, t2.id01, t2.id02, t3.curso, t3.id02
FROM
TABELA01 t1
INNER JOIN TABELA02 t2
ON t1.id01 = t2.id01
INNER JOIN TABELA 03 t3
ON t2.id02 = t3.id02
1
Let’s take an example:
You have an Article object that has more than one category, and a category may have more than one article.
In this example I want to pick up 10 articles with all categories.
SELECT * FROM artigo
LEFT JOIN artigo_categorias ON (artigo.id = artigo_categorias.artigo_id)
LEFT JOIN categorias ON (artigo_categorias.categoria_id = categoria_id)
LIMIT 10
In this case you could just make the call you make in PHP by calling this SQL.
Browser other questions tagged sql sql-server
You are not signed in. Login or sign up in order to post.
If you do not state which tables and which data from these tables, it is difficult to help.
– user28595
Use the
join
and the taste(inner
,right
etc) suitable for the expected results.– rray