开发者

mysql select multiple and join query

开发者 https://www.devze.com 2023-03-12 13:48 出处:网络
I have a complicated MYSQL query question here. I try my best to explain my problem. I have 4 tables. mid is a foreign key between the tables. table4 is NOT a compulsory table source. However I like

I have a complicated MYSQL query question here. I try my best to explain my problem.

I have 4 tables. mid is a foreign key between the tables. table4 is NOT a compulsory table source. However I like it returns all the rows even there are no match data from table4. So that I write a query script as following.

I'm not sure is it the logic way to write such 开发者_如何学Goquery script but what I know that the syntax is wrong.

SELECT * 
FROM table1, table2, table3, 
    (SELECT xx 
    FROM table4 
    RIGHT JOIN table1 ON table1.mid = table4.mid) 
WHERE table1.mid = table2.mid 
AND table1.mid = table3.mid 
AND tt = 'a' 
AND type = 1 
GROUP BY table1.mid 
ORDER BY xx DESC, table1.name ASC;


You have to do a left join between table1 & table4:

SELECT * 
  FROM table1
  JOIN table2 ON table1.mid = table2.mid 
  JOIN table3 ON table1.mid = table3.mid 
  LEFT JOIN table4 ON table1.mid = table4.mid 
 WHERE tt = 'a' 
   AND type = 1 
 GROUP BY table1.mid 
 ORDER BY xx DESC, table1.name ASC;
0

精彩评论

暂无评论...
验证码 换一张
取 消