yab86
yab86

Reputation: 405

Getting certain parts of a string out of a select

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

Answers (3)

Mahavir
Mahavir

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

Chrisi
Chrisi

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

Sherin Jose
Sherin Jose

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

Related Questions