Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with IN()

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I've stored lists of numbers in columns, that looks like this:
1,2,6,7
I've chosen varchar(255) to store these values. Let's call this column "numbers".

Now, I want to find out which rows holds a certain number. So, these two query should both return 1, right?

Query 1: SELECT 6 IN(1,2,6,7);
Query 2: SELECT 6 IN(numbers) FROM my_table;

The problem is that MySQL seems to interpret the second query like this:
SELECT 6 IN('1,2,6,7') FROM my_table;
...since "1,2,6,7" is stored as a string, so it doesn't work.

How can I make MySQL understand that I want it to be interpreted like QUERY 1?
 
Try

[tt]SELECT * FROM your_table WHERE the_column LIKE "%the_number%"[tt]

I hope it works... [sig][/sig]
 
I forgot to mention that the numbers will not always be one-digit.
But that's okay, 'cause I fixed it through the amazing powers of regular expressions, yaay!

SELECT * FROM my_table WHERE numbers REGEXP "(^|,)6(,|$)";
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top