Welcome to WebmasterWorld Guest from 34.204.36.101

Forum Moderators: open

Message Too Old, No Replies

select records that contain a certain value

Mysql query question

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

Junior Member

10+ Year Member

joined:Feb 4, 2007
posts:73
votes: 0


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

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

Junior Member

10+ Year Member

joined:June 6, 2005
posts:109
votes: 0


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.

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

Junior Member

10+ Year Member

joined:Feb 4, 2007
posts:73
votes: 0


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

Thanks again.

Russ