Forum Moderators: open

Message Too Old, No Replies

select records that contain a certain value

Mysql query question

         

russkern

11:14 pm on Sep 6, 2008 (gmt 0)

10+ Year Member



Hello all,

I need to write a MySql query that will only select entries from a database table where one of the columns only contains a certain character.

My "column A" contains an array of numbers seperated by comas (i.e. 1,3,5,7)

I only want to select records in which this "column A" contains a "3"

Can i do this through the query?
How is best to accomplish this?

Hope this makes sense..

Russ

MattAU

7:44 am on Sep 7, 2008 (gmt 0)

10+ Year Member



Yep, you can do this. You just need to be a bit careful that searching for '3' doesn't return a when the match is '23'.. Assuming you wouldn't want that match, something like this should do the trick:

SELECT * FROM table WHERE columna = '3' OR columna LIKE '3,%' OR columna LIKE '%,3,%' OR columna LIKE '%,3'

You could also use a regular expression, but I reckon this method would be quicker.

russkern

11:17 am on Sep 7, 2008 (gmt 0)

10+ Year Member



That worked great... I was actually thinking along those lines, but never quite got it...

Thanks again.

Russ