0

i have this sting for example: 11,22,33

and this is me table:

ID   /   content
---------------------
1    /  11,15,18,20,22,28,29,30,
2    /  10,11,17,20,22,27,33,40,
3    /  4,8,9,15,22,23,24,31,33,
4    /  10,11,20,22,30,33,40,41,
5    /  5,6,7,10,11,22,28,30,33,
6    /  10,11,12,13,14,15,20,22,

id 1 contains only 11 and 22

id 2 contains all items from strin: 11 and 22 and 33

id 3 contains only 22 and 33

id 4 contains all items from strin: 11 and 22 and 33

id 5 contains all items from strin: 11 and 22 and 33

id 6 contains only 11 and 22


How to create me MYSQL command to read only items with ID: 2,4,5

SELECT *
FROM table
WHERE content ???
5
  • 9
    This would be a lot easier if the database was normalized. You're running into the exact same problem everybody has when they try to store multiple values in a single column. Commented Mar 6, 2015 at 10:59
  • 1
    What is your expected result? Commented Mar 6, 2015 at 11:01
  • SELECT t.* FROM table t WHERE t.content LIKE '%11,%' AND t.content LIKE '%22,%' AND t.content LIKE '%33,%'... It's not a good db structure Commented Mar 6, 2015 at 11:08
  • 1
    @Javaluca That would not work. You also find entries with 222,333,111, Commented Mar 6, 2015 at 11:35
  • 1
    SELECT t.* FROM table t WHERE (t.content LIKE '11,%' OR t.content LIKE '%,11,%' ) AND (t.content LIKE '22,%' OR t.content LIKE '%,22,%' ) AND (t.content LIKE '33,%' OR t.content LIKE '%,33,%' ).. better? Commented Mar 6, 2015 at 12:06

1 Answer 1

1

Use find_in_set

SELECT *
FROM table
WHERE find_in_set ('11',content) and find_in_set ('22',content) and find_in_set ('33',content)

For more information read the documentation

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.