【发布时间】:2017-02-16 00:24:59
【问题描述】:
我想同时选择 DISTINCT(p.ptype) 如果 p.ptype 不在 c.ptype 的集合中,我也想获取 c.category
数据库表:p
id ptype
1 Shirts
2 Cups
3 Shirts
4 Mugs
数据库表:c
id category ptype
1 Test Pants, Shirts, TShirts
2 Test1 Cups, Mats, Rugs
我试过的SQL命令如下
SELECT DISTINCT(p.ptype), IF(FIND_IN_SET(p.ptype, c.ptype), c.category,'') as category
FROM p, c
这会输出 p.ptype 两次设置。一次是空白的 c.category 字段,一次是填充的 c.category。
但是想要的输出如下
ptype category
Shirts Test
Cups Test1
Mugs
【问题讨论】:
标签: mysql select distinct find-in-set