user1031721
user1031721

Reputation:

How to use LIKE in MySQL?

I got a table called music with some ids:

 6983887641 59088763306 116542622632 106436032725763 6750402929 131572223581891 17337462361 56848544614 108089415891178 27659711968 182011613865 8888384363 43780515976 7872609449 38858586087 107901192572009 9028468518 5461947317 11955325695 64075031411 12673567174

And i need to do a query on those ids. If a query like this had been done:

SELECT username FROM music WHERE music LIKE '%107901192572009 9028468518%'

It would return with the username of the one that has these ids in it's music table. Ok. but the problem is if i add ids in different order there is no result. so if i searched for:

56848544614 107901192572009 or 5461947317 9028468518

They are in the table, but not in the order, and there is no result. What should i do?

Upvotes: 0

Views: 84

Answers (2)

Adrian Cornish
Adrian Cornish

Reputation: 23848

I think you do not want LIKE you want IN

 SELECT username FROM music WHERE music IN (6983887641, 59088763306, 116542622632, 106436032725763);

Upvotes: 0

Amadan
Amadan

Reputation: 198304

SELECT username FROM music
WHERE music LIKE '%107901192572009%'
  AND music LIKE '%9028468518%'

Note that it will be slow. Also note that it can misfire, since an id can be a part of another id. There is a better way to do this - look up 1-to-n relations. If not, at least consider doing this (to combat the second problem):

SELECT username FROM music
WHERE ' ' + music + ' ' LIKE '% 107901192572009 %'
  AND ' ' + music + ' ' LIKE '% 9028468518 %'

Upvotes: 1

Related Questions