0

i have a column in my database with string value like "1,2,3,4,5,". I want to compare and find an integer value I defined before in that string. But I want this string to have the purpose of an array. There is no problem up to here. I am finding the integer in array like that:

$sql='SELECT * FROM '. TOPIC_TABLE .' WHERE multi_dept LIKE \'%'.$prjId.',%\' ORDER BY topic';

but, when there is a topic have id 11 in the string, there will be an error.

{1,2,11,}-> when i search ID=1 in the string, it will find also 1 and 11. Is there anyway to avoid and solve this problem with SQL query?

1
  • 1
    Yeah, you can normalize your data. Commented May 15, 2012 at 7:44

2 Answers 2

3

As you have a , after every integer, isn't this as easy as using the WHERE clause below?

WHERE multi_dept LIKE '$prjId,%' XOR multi_dept LIKE '%,$prjId,%'
Sign up to request clarification or add additional context in comments.

Comments

0

FIND_IN_SET to the rescue.

 $sql='SELECT * FROM '
     . TOPIC_TABLE
     . ' WHERE FIND_IN_SET( '
     . $prjId
     . ', multi_dept ) ORDER BY topic'

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.