假设我有3张桌子;
tableA 列id,价格,目录;
tableB 包含列ID,销售,信息,数据;
tabeC ,其中包含列id,name,desc
id 列与所有3个表
相关如何在一个MySQL语句中获得如下表格?
id price catalogue B C AESDR 2000 We are mayhem 0 1 DRETH 1400 Let's go a fishing 1 0
上面的列 B 和 C 根据 tableB 中是否存在项目而保留值0或1 tableC 表
'请注意,前三列来自 tableA
答案 0 :(得分:0)
像这样:
SELECT
TableA.id, TableA.price, TableA.catalogue,
IF(TableB.id IS NULL, 0, 1) AS B,
IF(TableC.id IS NULL, 0, 1) AS C
FROM TableA LEFT JOIN TableB ON TableA.id = TableB.id
LEFT JOIN TableC ON TableA.id = TableC.id
答案 1 :(得分:0)
左连接是你的朋友:
SELECT
a.*
if(b.id is null, 0, 1) as B,
if(b.id is null, 0, 1) as C
FROM tableA a
LEFT JOIN tableB b ON b.id = a.id
LEFT JOIN tableC c ON c.id = a.id