SQL - list of merged contend of two (joined) tables [duplicate]

107
November 21, 2018, at 10:50 PM

This question already has an answer here:

  • Selecting all items in one table and join with another table, allowing nulls 4 answers

team,

i am currently lost ..

what i wanna do is a kind of JOIN, however a SELECT using a JOIN will skip rows when the condition is emty ...

an UNION will end in an error-message as the select over T1 comes with 4 and T2 with 2 lines in my example below:

T1: Name        Ort
---------------------- 
Markus      München 
Christian   Berlin 
Hubert      Frankfurt 
Kalle       Düsseldorf
T2
Ort         Anmerkung
-----------------------
München     Bayern
Frankfurt   Hessen

Outcome what i wanna archive are all 4 lines from T1 including the information from T2 (when exists) - compared/joined over "Ort".

Result:
T1.Name     T1.Ort      T2.Anmerkung
------------------------------
Markus      München     Bayern
Christian   Berlin      
Hubert      Frankfurt   Hessen
Kalle       Düsseldorf
Answer 1

Use LEFT JOIN. It will keep records from the first table that don't have a corresponding row in the second table.

select t1.name, t1.ort, t2.anmerkung
from t1
left join t2 on t1.ort=t2.ort
READ ALSO
Very slow MySQL query performance

Very slow MySQL query performance

I've a query that takes about 18 seconds to finish:

140
Join self table and get values in new rows

Join self table and get values in new rows

I wonder if there is a way to JOIN a self table and then get those values in new rows

88
Alternative to a different MYSQL table for each file my user uploads? [on hold]

Alternative to a different MYSQL table for each file my user uploads? [on hold]

I built an application for personal use that I am now looking to sell commerciallyThe application is essentially a tool that accepts a CSV file with anywhere from 10k-270k rows and then generates a report using the information inside

69