我有两张桌子,我需要展示additionalcolors
只有 1 个玩具图案的玩具。示例(FW18、FK97、FK38、LX74)。我使用了一个左外连接 from Toytypes
toToyadditionalcolor
并执行了一个子查询来查找只有一个玩具名称的玩具模式。但我仍然无法获得像下面的结果查询这样的结果。
请告诉我我需要修改代码的哪一部分以获得与下面的 query1 表类似的结果?
玩具类型
ToyPattern ToyName mainColor
---------- --------------- ---------
F692 Dino pink
F692 Elephant pink
FK38 Elephant pink
FK97 Giraffe purple
FW18 Sonic pink
LX73 Kangaroo pink
LX73 Cow blush
LX73 Dog pink
LX74 Cat plum
玩具附加颜色
ToyPattern ToyName firstColor additionalColor
---------- ---------- ----------- ----------------
FK38 Elephant pink orange
FK38 Elephant pink yellow
LX74 cat plum dark pink
LX74 cat plum pale pink
期望的输出:
ToyPattern ToyName color 1 color 2 color 3 color 4 color 5
----------- ---------- ------------ ------------ ------------ ------------ ------------
FK38 Elephant pink orange yellow NULL NULL
FK97 Giraffe purple NULL NULL NULL NULL
FW18 Sonic pink NULL NULL NULL NULL
LX74 cat plum dark pink pale pink NULL NULL
下面是我的 SQL 代码。
select distinct
toytypes.toypattern,
toyname,
toytypes.flowerBreed,
toytypes.firstColor as 'color 1'
from
Toytypes
left join
ToyAdditionalColor on ToyAdditionalColor.toypattern = Toytypes.toypattern
where
toytypes.toypattern in
(select Toypattern
from Toytypes
group by toypattern
having count(toypattern) < 2)