MYSQL learning two about left join

 

There is the following SQL in the work, for A.ID = 'abcdefg', left join B and C two tables, to find other information. Even if there are no records in B and C that meet the conditions, the final result is definitely not empty. Because A.ID = 'abcdefg' exists.

SELECT ******
FROM tableA    A
LEFT JOIN tableB  B  on b.DELETED = '0'
AND A.DELETED = '0'
AND B.fid=A.ID
LEFT JOIN tableC  C ON B.XXXid=C.id AND c.DELETED = '0'
WHERE A.ID ='abcdefg'

At the beginning, I wrote it like this: Put b.DELETED = '0' at the back, which leads to a null result, this is because: If you put and b.DELETED = '0' at the end, it is after the left join The result is filtered,

At this time, the result may be null. (Consider this situation: Tables B and C do not have records that can be successfully connected to A. If the above SQL is used, the result will be displayed, and if this SQL is used, it will be null)

SELECT ******
FROM tableA    A
LEFT JOIN tableB  B  on
AND A.DELETED = '0'
AND B.fid=A.ID
LEFT JOIN tableC  C ON B.XXXid=C.id AND c.DELETED = '0'
WHERE A.ID ='abcdefg' 

and  b.DELETED = '0'

 

 

The above problem is encountered at work, and I accidentally wasted a lot of time to fix it, and I have a bad grasp of SQL!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325220215&siteId=291194637