Jump to content

List of Values in Column

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
Chan

Chan

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 204 posts
I will have a list of values in a column on my table. I need to write a SQL query that detects if the value is in the column. An example of the data would be:

8,9,20,44,39,82

and say the value was 44. Is there a regex express I could use in PHP/SQL to determine if the value is actually present?

#2
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
Can't you just do something like this?
//if the query is successful, the value is present...
if(mysql_query('SELECT column FROM table WHERE column=44'))
$value = true;


#3
Guest_Jordan_*

Guest_Jordan_*
  • Guests
If you are asking how to determine if the value '44' is in a field that contains all of those values you could easily use regexp.

SELECT * from table
WHERE field REGEXP '44,'
;

More Info: MySQL AB :: MySQL 5.0 Reference Manual :: 12.4.2 Regular Expressions

#4
Chan

Chan

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 204 posts
The regexp works but I had to add a comma before and after:

,10,89,13,47,

because with your REGEXP it never found the last ID (no ending comma) and it would find numbers that ended with '44'

#5
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
Of course the REGEXP works, Jordan is a genius. :)

#6
Guest_Jordan_*

Guest_Jordan_*
  • Guests

Chan said:

The regexp works but I had to add a comma before and after:

,10,89,13,47,

because with your REGEXP it never found the last ID (no ending comma) and it would find numbers that ended with '44'

Yeah, I didn't think about that but if adding a starting and ending comma is no problem I think this method is solid.

Sidewinder said:

Of course the REGEXP works, Jordan is a genius. :)

Well thank you! :D

#7
iqstaffing

iqstaffing

    Newbie

  • Members
  • Pip
  • 3 posts
You could have also used MYSQL 'LIKE' function

SELECT * from table

WHERE field LIKE '%,47,%' 

;

IQStaffing - IT Recruiter
- Need an IT job? let us find you one!
http://www.iqstaffing.com