Cerca group_concat usando like
-
25-09-2019 - |
Domanda
Ho una query SQL che utilizza Group_Concat per far attaccare tutte le persone a un determinato ordine. C'è un modo per cercare nel campo 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
Voglio aggiungere qualcosa di simile WHERE attachedContacts LIKE '%Eric%'
, per elencare solo gli ordini con "Eric" allegato, ma includono comunque tutti gli altri contatti nella query.
La query restituisce dati come:
orderID atachedContacts
01 Eric Siegel, John Smith
02 Jason Jackson, Bill O'Neil
03 Eric Siegel, Jason Jackson, Neil O'Ryan
Voglio che la query restituisca le righe 01 e 03 perché "Eric" è nell'elenco dei contatti.
Come posso fare questo?
Soluzione
Prova questo:
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%'
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow