-2

I am trying to search on specific scenario from last 8 hours but unable to sort it out.

I need to get records from single table where I need to match a comma separated string against two columns.

-Both columns contain single values like 1 or comma separated values like 1,2,3

I need to get records where minimum one AND condition matches for both columns either for single value or comma separated value.

Here is my query

SELECT specialities, ids_origin, id, latitude, longitude 
FROM `ep_restaurant` 
where `specialities` in (2,4,5,32) and `ids_origin` in (106,154,3)

The record is fetching

http://prntscr.com/ntiao7

But it is matching when both columns have same whole set of comma separated values, I also need to get all of those where even single values from both columns match like 2,6 or 2,154 or whole comma separated string like (2,4,5,32) and (106,154,3) matched..

It is not my own database so i cannot change it. Please help me .Thanks.

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
dev
  • 555
  • 1
  • 13
  • 31
  • 2
    Fix your data model! Don't store comma separate values in columns. Use junction/association tables. – Gordon Linoff May 26 '19 at 13:12
  • Take some time to read about [Database normalization](https://en.wikipedia.org/wiki/Database_normalization). – AymDev May 26 '19 at 13:23
  • @GordonLinoff it is not my own database so i cant change it, is there any solution with this database? – dev May 26 '19 at 14:16
  • @AymDev it is not my own database, is there any solution with this database? – dev May 26 '19 at 14:16
  • I reopened this question. The duplicate explains *why* the data model is bad and how to fix it, but the OP apparently has no control over the data model. S/he still has a problem of matching separate comma-delimited lists. – Gordon Linoff May 26 '19 at 15:05

1 Answers1

0

MySQL has a useful function for such must-avoid use-case.

For example, if you have specialities table:

SELECT r.id, latitude, longitude, r.specialities
  FROM ep_restaurant r
  WHERE exists( SELECT 1 
                  FROM specialities s 
                  WHERE find_in_set(s.id, r.specialities)
                    AND s.id in(2,6)
              )
     OR exists( SELECT 1 
                  FROM <Origins Table> o 
                  WHERE find_in_set(o.<Column Id>, r.ids_origin)
                    AND o.<Column Id> in(106,154)
              )
Sal
  • 1,307
  • 1
  • 8
  • 16
  • thanks but there are two columns ids_origin too. Do i need to write same sub query for that too? it would be very helpful if you adjust the second column in your code too\ – dev May 26 '19 at 14:33
  • i have applied your solution and i think it works SELECT r.id, latitude, longitude, r.specialities,r.ids_origin FROM ep_restaurant r WHERE exists( SELECT 1 FROM ep_restaurant s WHERE find_in_set(s.id, r.specialities) AND s.id in(2,4,5,32) )and( SELECT 1 FROM ep_restaurant i WHERE find_in_set(i.id, r.ids_origin) AND i.id in(106,154,3) ) But it is returning only1 result from both sub queries – dev May 26 '19 at 14:38
  • Yes, add another `exists` for the second column. I just could not figure out the name of second table -if exists. – Sal May 26 '19 at 14:41
  • i have added and posted it above. Both sub queries are returning 1 results what about other results that were matched ? how to display them? – dev May 26 '19 at 14:47
  • Template added for second column. – Sal May 26 '19 at 14:57
  • Both sub queries are returning 1 results what about other results that were matched ? how to display them? – dev May 26 '19 at 15:14
  • You need to provide more info. A complete MVE. – Sal May 26 '19 at 15:24
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/193951/discussion-between-dev-and-sal). – dev May 26 '19 at 15:36
  • i have provided all details in the question. Can you please read again? there is only one table. – dev May 26 '19 at 16:17
  • can you reply please. – dev May 27 '19 at 06:54
  • You still need to provide more sample data. Currently we have only one record of the only one table. See [this](https://stackoverflow.com/help/minimal-reproducible-example) – Sal May 27 '19 at 14:01