tags:

views:

71

answers:

3

i want to make a mysql selection but i need to select two types of data, one starting with 0256 and other with 0356. can i use:

SELECT * FROM table WHERE tel LIKE '0256%' AND '0356%' ?

thanks, Sebastian

EDIT i have a problem with these queries in PHP. the query above, works fine in mysql, but in PHP i get results only for LIKE '0256%'

+10  A: 

You are probably looking for a query like this:

SELECT *                /* Select all columns */
FROM table              /* from the table named 'table' */
WHERE tel LIKE '0256%'  /* where the field 'tel' starts with '0256' */
   OR tel LIKE '0356%'  /* or where the field 'tel' start with '0356' */
Greg
+4  A: 

You had 2 problems using AND instead of OR, and not having LIKE before the second parameter

SELECT * FROM table WHERE tel LIKE '0256%' OR tel LIKE '0356%' 
Shiraz Bhaiji
you need an additional `tel` after `OR`.
Vlad
@Vlad, thanks good catch
Shiraz Bhaiji
A: 

You can also use a REGEX for this:

SELECT * 
FROM table 
WHERE tel RLIKE '0[23]56.*'

This may make things clearer as more digit options get added (assuming they will). It might also make the query faster, but profiling would have to be performed to see if this is in fact the case.

Michael Goldshteyn
Regex in MySQL ensures a table scan - unless wildcarding the left side, the LIKE alternative will not so it's more efficient.
OMG Ponies
I didn't know that MySQL's regex optimizations were this limited, but hopefully in future versions the opportunities for optimizations will increase.
Michael Goldshteyn