Search GROUP_CONCAT using LIKE

社会主义新天地 提交于 2019-12-10 14:16:59

问题


I have an SQL query that uses GROUP_CONCAT to get all people attached to a certain order. Is there a way I can search inside the GROUP_CONCAT field?

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

I want to add something like WHERE attachedContacts LIKE '%Eric%', to only list orders with 'Eric' attached, but still include all other contacts in the query.

The query returns data like:

orderID atachedContacts
01      Eric Siegel, John Smith
02      Jason Jackson, Bill O'Neil
03      Eric Siegel, Jason Jackson, Neil O'Ryan

I want the query to return rows 01 and 03 because 'Eric' is in the contact list.

How can I do this?


回答1:


Try this:

SELECT orders.orderID, 
GROUP_CONCAT(contacts.firstName, " ", contacts.lastName) AS attachedContacts
FROM orders
JOIN contacts ON orders.contactID=contacts.contactID
GROUP BY orders.orderID DESC
HAVING attachedContacts LIKE '%Eric%'


来源:https://stackoverflow.com/questions/3806830/search-group-concat-using-like

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!