Tanvir Alam Tanvir Alam - 1 year ago 66
MySQL Question

Issues with SQL queries

I have 2 tables and result as shown in the image below: MySQL DB

enter image description here

What would be best way to join the two tables so we get the result as shown above.

SELECT * FROM (SELECT id, desc FROM table2) as T1
LEFT JOIN (SELECT * FROM table1) as T2 ON T1.id = T2.id

I guess my SQL is not working.

Answer Source

You can use a LEFT JOIN with COALESCE:

SELECT t1.id, COALESCE(t2.desc, t1.desc) AS desc, t1.D1, t1.D2
FROM table1 as T1 
LEFT JOIN table2 as T2 ON T1.id = T2.id
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download