I have a little knowledge on table relationships, I have read the answers to the question on #
Own experience:
Depending on how you diagram yourbd
, they can have n
tables, and depending on the need of your query
, you may need to use a table as the main one of your query, exemplifying with a very small layout ornament:
IntheexampleIhave6tables,thetabela3
clearlyisthemainoneofmysystem,itconnectswithalltheothers,itcouldbeatableofplans,forexample...
IfbychanceIwanttocreateonlyaquery
bybringinginformationregardingid
oftabela3
,andotheradditionalinformationfromothertables,forexampleImightwanttobringallinformationregardingtabela4
,buthasnoconnectionwithtabela2
and6,soIcouldusetheresourcesofbothleftjoin
andrightjoin
sothatbothcouldmeetmyneed.
Inmathematicalterms,recallingthatsetofsets,itmaybeeasiertovisualize:
Inthisexample,Ineedtogetinformationthatisprimarilyintabela1
andnotintabela2
,andareprimarilyintabela5
butnotintable6.
Therearetwowaysofdoingsuchaquery,oneusingsub-querys
(Themostrecommendedwhenthereareconditionsinthe"sub-domain" query, which do not depend only on the relationship of the tables), or another that is using the resource of left join
and right join
to bring only the data that is needed from one or another table, in that case the select would look something like this [edit]
In terms of operation there is no difference only in terms of concept as the name says righ or left right or left if you use
SELECT *
FROM tabela_a a
LEFT JOIN tabela_b b
ON b.nome = a.nome;
You will get all the results of the table that is on the left, this tabela_a
even those that do not match the table on the right. In case if there is no comparison it brings null
.
If you traded for right it would fetch all results of tabela_b
(which is to the right of table A).