我有一个SQL查询,它使用GROUP_CONCAT来使所有人都附加到某个订单.有没有办法在GROUP_CONCAT字段内搜索?
SELECT orders.orderID,GROUP_CONCAT(contacts.firstName," ",contacts.lastName) AS attachedContacts
FROM (orders)
JOIN contacts ON orders.contactID=contacts.contactID
GROUP BY orders.orderID
ORDER BY orders.orderID DESC
我想添加像WHERE attachContacts LIKE’%Eric%’这样的东西,只列出附加’Eric’的订单,但仍然包括查询中的所有其他联系人.
查询返回如下数据:
orderID atachedContacts
01 Eric Siegel,John Smith
02 Jason Jackson,Bill O'Neil
03 Eric Siegel,Jason Jackson,Neil O'Ryan
我希望查询返回行01和03,因为’Eric’在联系人列表中.
我怎样才能做到这一点?
最佳答案
试试这个:
SELECT orders.orderID,contacts.lastName) AS attachedContacts
FROM orders
JOIN contacts ON orders.contactID=contacts.contactID
GROUP BY orders.orderID DESC
HAVING attachedContacts LIKE '%Eric%'