我有三张桌子
颜色
date,id ,highlightedcolor
形状
date,id,highlightedshapes
身高
date,id, highlightedheight
所有这些表都有不同的行数,但共享唯一ID
我需要一个查询来从三个表中选择不同的日期和相应的id,其中hightlightedcolor为黄色,highlightshapes为正方形,高亮显示为短
非常感谢任何帮助
尝试
SELECT DATE,ID ,HIGHLIGHTEDCOLOR FROM COLORS WHERE HIGHLIGHTEDCOLOR ='YELLOW' UNION SELECT DATE,ID ,HIGHLIGHTEDSHAPE FROM SHAPE WHERE HIGHLIGHTEDSHAPE ='SQUARE' UNION SELECT DATE,ID ,HIGHLIGHTEDHEIGHT FROM HEIGHT WHERE HIGHLIGHTEDHEIGHT ='SHORT'
错误
错误:ER_WRONG_NUMBER_OF_COLUMNS_IN_SELECT:使用的SELECT语句具有不同的列数
答案 0 :(得分:0)
使用join
和distinct
Select distinct t1.date,
t1.id,
t1.hightlightedcolor,
t2.highlightedshapes,
t3.highlightedheight
from color t1
INNER JOIN shapes t2 ON t1.Id = t2.Id
INNER JOIN height t3 ON t1.Id = t3.Id
WHERE t1.hightlightedcolor = 'yellow'
AND t2.highlightedshapes ='square'
AND t3.highlightedheight ='short'
答案 1 :(得分:0)
这将是最短的解决方案:
SELECT DISTINCT c.date, c.id
FROM color c
shapes s
height h
WHERE c.id = s.id
AND s.id = h.id
AND c.hightlightedcolor = 'yellow'
AND s.highlightedshapes ='square'
AND h.highlightedheight ='short'