使用LIKE搜索GROUP_CONCAT [英] Search GROUP_CONCAT using LIKE

查看:221
本文介绍了使用LIKE搜索GROUP_CONCAT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个SQL查询,它使用GROUP_CONCAT将所有人连接到特定的订单。有什么方法可以在GROUP_CONCAT字段内搜索?

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

我想添加类似于 WHERE attachedContacts LIKE'%Eric%' ,只列出附有'Eric'的订单,但仍包含查询中的所有其他联系人。

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.

查询返回如下数据: / p>

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

我想要查询返回行01和03,因为'Eric'在联系人列表中。

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

我该怎么做?

推荐答案

试试这个:

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%'

这篇关于使用LIKE搜索GROUP_CONCAT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆