我有这两个表:
表1:
ID ITEM
--------------------
1 AB
1 22S
1 AB
2 F45
2 BB
3 1
3 1
3 AA
3 F45
3 F67
3 A
......
表2:
ITEM COUNTRY
---------------
0 usa
1 italy
2 mexico
3 mexico
A greece
AA malta
AB usa
AC pakistan
B uk
BB france
BA france
BB russia
F45 uk
我使用以下查询来获取类似" A%"的项目。对于每个ID:
SELECT GROUP_CONCAT(ITEM)
FROM (SELECT Table1.ID, Table1.ITEM, Table1.date
FROM Table2 Table2 INNER JOIN Table1 Table1
ON (Table2.ITEM = Table1.ITEM) WHERE table2.ITEM like "A%"
ORDER BY Table1.id, Table1.date, Table2.ITEM) as temp
GROUP BY ID
ORDER BY ID
我想找到一种方法来编辑我的查询,这样我就可以得到上面只有碰巧有项目的ID" F45"在他们(但我不想使用这个项目,只需选择有它的ID).. 任何帮助将不胜感激
答案 0 :(得分:1)
我想
SELECT GROUP_CONCAT(ITEM) AS group_item
FROM (SELECT Table1.ID, Table1.ITEM, Table1.date
FROM Table2 Table2 INNER JOIN Table1 Table1
ON (Table2.ITEM = Table1.ITEM) WHERE table2.ITEM like "A%"
ORDER BY Table1.id, Table1.date, Table2.ITEM) as temp
GROUP BY ID
HAVING group_item LIKE '%F45%'
ORDER BY ID
或调整子查询以仅选择所需的行(此处我不确定您要实现的目标)