I have 3 tables :
tb_a tb_b tb_c
============== ============== ==============
|id|doc1|urla| |id|doc2|urlb| |id|doc3| sum|
============== ============== ==============
| 1| c1 |url1| | 1| c2 |urla| | 1| c2 |sum1|
| 2| c3 |url3| | 2| c4 |urlb| | 2| c3 |sum3|
| 3| c2 |url2| | 3| c6 |urlc| | 3| c4 |sum4|
============== ============== | 4| c6 |sum6|
==============
First, I wanna check tb_a and tb_b. If there’re same data in doc1 and doc2, I just use data doc1 in tb_a. here’s first query :
SELECT
a.doc1
FROM
tb_a a
LEFT JOIN
tb_b b
ON a.doc1 = b.doc2
WHERE
b.doc2 IS NULL
then left join with tb_c to get sum. can I make those processes in one query? how? thank you 🙂
from the example, so the result must be :
c3 url3 sum3
c2 url2 sum1
c4 urlb sum4
c6 urlc sum6
You can join all the tables using
LEFT JOINwithCOALESCE.SQLFiddle Demo