Difference between RIGHT JOIN and LEFT JOIN [duplicate]

6

I have a little knowledge on table relationships, I have read the answers to the question on #

asked by anonymous 27.08.2016 / 02:21

2 answers

4

Own experience:

Depending on how you diagram your bd , 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,thetabela3clearlyisthemainoneofmysystem,itconnectswithalltheothers,itcouldbeatableofplans,forexample...

IfbychanceIwanttocreateonlyaquerybybringinginformationregardingidoftabela3,andotheradditionalinformationfromothertables,forexampleImightwanttobringallinformationregardingtabela4,buthasnoconnectionwithtabela2and6,soIcouldusetheresourcesofbothleftjoinandrightjoinsothatbothcouldmeetmyneed.

Inmathematicalterms,recallingthatsetofsets,itmaybeeasiertovisualize:

Inthisexample,Ineedtogetinformationthatisprimarilyintabela1andnotintabela2,andareprimarilyintabela5butnotintable6.

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]

    
27.08.2016 / 03:51
3

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).

    
27.08.2016 / 02:34