我有以下加入的查询:
SELECT t1.*, t2.*
FROM TabContent t1
LEFT JOIN HighScore t2
ON t1.Name = t2.Layout
WHERE TabID IN (1,2,3)
ORDER BY TabID, LayoutName
结果:
+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+
|TabConID| Name | Tiles | Layers | TabID |Score ID| fbID | Layout | Score | Time | Stars | Date |
+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+
| 1 | beetle | 30 | 5 | 1 | 1 | 2222 | beetle | 293 | 13:00 | 1 | date |
| 2 |elephant| 31 | 4 | 1 | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| 3 | dragon | 31 | 5 | 2 | 2 | 3333 | dragon | 232 | 08:35 | 2 | date |
+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+
现在我要显示fbID 2222拥有的所有名称(以及随附的数据)。我是这样做的:
SELECT t1.*, t2.* FROM TabContent t1 LEFT JOIN HighScore t2 ON t1.Name = t2.Layout WHERE fbID = 2222 AND TabID IN (1,2,3) ORDER BY TabID, LayoutName
结果:
+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+
|TabConID| Name | Tiles | Layers | TabID |Score ID| fbID | Layout | Score | Time | Stars | Date |
+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+
| 1 | beetle | 30 | 5 | 1 | 1 | 2222 | beetle | 293 | 13:00 | 1 | date |
+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+
到目前为止一切顺利!问题是,有更多的名字(大象,龙)。我也希望显示这些,但保留数据(接受TabConID,名称,平铺,图层,TabID)为空白。
这是我需要的结果:
+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+
|TabConID| Name | Tiles | Layers | TabID |Score ID| fbID | Layout | Score | Time | Stars | Date |
+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+
| 1 | beetle | 30 | 5 | 1 | 1 | 2222 | beetle | 293 | 13:00 | 1 | date |
| 2 |elephant| 31 | 4 | 1 | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
| 3 | dragon | 31 | 5 | 2 | NULL | NULL | NULL | NULL | NULL | NULL | NULL |
+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+
最好的方法是使用UNION。如果这是一个普通的Query而不是一个连接的查询,那么查询看起来就像这样(这是一个非连接的例子,我不是在使用,而是为了解释一些事情):
SELECT * FROM `table` WHERE fdID = 2222 AND TabID IN (1,2,3) ORDER BY TabID, LayoutName
UNION
select TabConID, Name, Tiles, Layers, TabID, NULL, NULL, NULL, NULL, NULL, NULL, NULL FROM `table` where fbID != 2222
不幸的是,这是一个加入的查询,我不知道如何使UNION部分与此一起工作。也使用谷歌搜索但没有运气。
希望有人可以提供帮助!
答案 0 :(得分:1)
您必须更改查询并将ORDER BY置于底部,以下内容应该起作用:
SELECT * FROM `table` WHERE fdID = 2222 AND TabID IN (1,2,3)
UNION
select TabConID, Name, Tiles, Layers, TabID, NULL AS Score ID, NULL AS fbID, NULL AS Layout, NULL AS Score FROM `table` where fbID != 2222
ORDER BY TabID, LayoutName
答案 1 :(得分:1)
您不需要执行UNION查询。只需将“fbId”条件从WHERE移动到JOIN条件。
以下查询应该为您提供所需的结果:
SELECT t1.*, t2.*
FROM TabContent t1
LEFT JOIN HighScore t2
ON t1.Name = t2.Layout AND t2.fbId = 2222
WHERE TabID IN (1,2,3)
ORDER BY TabID, LayoutName
希望它有所帮助!
答案 2 :(得分:0)
您不能在UNION之前拥有WHERE子句。
要做你想做的事,你可能需要将联盟的第一个“腿”挤入子查询中。