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?
List of Values in Column
Started by Chan, Jul 16 2007 07:37 AM
6 replies to this topic
#1
Posted 16 July 2007 - 07:37 AM
|
|
|
#2
Posted 16 July 2007 - 12:33 PM
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_*
Posted 16 July 2007 - 05:17 PM
Guest_Jordan_*
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.
More Info: MySQL AB :: MySQL 5.0 Reference Manual :: 12.4.2 Regular Expressions
SELECT * from table WHERE field REGEXP '44,' ;
More Info: MySQL AB :: MySQL 5.0 Reference Manual :: 12.4.2 Regular Expressions
#4
Posted 17 July 2007 - 04:22 PM
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'
,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
Posted 17 July 2007 - 09:39 PM
Of course the REGEXP works, Jordan is a genius. :)
#6
Guest_Jordan_*
Posted 18 July 2007 - 02:18 PM
Guest_Jordan_*
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'
,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
Posted 16 August 2007 - 03:37 AM
You could have also used MYSQL 'LIKE' function
SELECT * from table WHERE field LIKE '%,47,%' ;


Sign In
Create Account


Back to top









