Reputation: 405
I have a database (mydb) with following data in one column:
PK:
I have to compare these data with a variable $id
for example: 2724296
SELECT * FROM mydb WHERE PK = '$id';
Of course, this query won't deliver a result. Is there a easy way to compare my $id with the values in the brace and the values with no brace? A replace in the select replaces e.g. only the brace, but not the value left to the brace.
Greets, Yab86
Upvotes: 1
Views: 52
Reputation: 332
You can use below:
SELECT * FROM mydb WHERE PK LIKE '%(2724296)';
Below is little complex: using regular expression
SELECT * FROM mydb WHERE PK REGEXP '[(]2724296[)]';
See this https://dev.mysql.com/doc/refman/5.1/en/regexp.html for writing customizable regular expression.
Upvotes: 1
Reputation: 381
I would try a regular expression (assuming you only want to check the number inside the brackets). Something like this should work:
\([0-9]*\)$
This will return a string that begins with ( followed by 1 or more numbers and ends with ). You should then be able to remove the brackets from the string and compare it with your variable
Upvotes: 0
Reputation: 2526
You can use REGEXP for this. Try this query,
$query = "SELECT * FROM mydb WHERE PK REGEXP '({$id})' OR PK REGEXP '-{$id}'";
Upvotes: 1