选择不同于三个表的查询

问题描述 投票:2回答:2

我有三张桌子

颜色

date,id ,highlightedcolor 

形状

date,id,highlightedshapes

高度

date,id, highlightedheight

所有这些表都有不同的行数,但共享唯一的id

我需要一个查询来从所有三个表中选择不同的日期和相应的id,其中hightlightedcolor为黄色,突出显示的形状为正方形,高亮显示为短

任何帮助深表感谢

试着

   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语句具有不同的列数

mysql sql
2个回答
0
投票

使用joindistinct

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'

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'
© www.soinside.com 2019 - 2024. All rights reserved.