Jump to content

Using Wildcards In MySQL IN Statement


Zergman

Recommended Posts

I have a column that stores 10 digit phone numbers from different provinces.  What i'm trying to do is to extract all phone numbers from 1 province.  This province has 3 different area codes.

 

Here's what I have so far.

<?php
$sql = "
SELECT dslam, COUNT(*) as total
FROM NS_data 
WHERE stn IN ('780%','403%','587%')
AND tdate = '$date'
GROUP BY dslam 
ORDER BY total DESC 
";
$res = mysql_query($sql);
while (list($id, $tot) = mysql_fetch_row($res))
{
    echo "$id - $tot <br />";
}
?>

 

Problem as you can see is apparently you can't use wildcards with IN

WHERE stn IN ('780%','403%','587%')

 

Not sure how to make it work

Link to comment
https://forums.phpfreaks.com/topic/199880-using-wildcards-in-mysql-in-statement/
Share on other sites

Good stuff everyone, thanks for the advise and info!

 

Problem i'm running into now is that I can't figure out the proper way to do the entire query.

 

This works for grabbing all numbers in the 3 area codes.

SELECT stn, tdate
FROM NS_data
WHERE stn LIKE '780%'
OR stn LIKE '403%'
OR stn LIKE '587%'

 

But when I do this

SELECT stn, 
tdate 
FROM NS_data 
WHERE stn LIKE '780%' 
OR stn LIKE '403%' 
OR stn LIKE '587%'
AND tdate = '$date'

 

It doesn't narrow down by the $date variable which is just set to todays date.

 

I'm guessing its got something to do with using OR's and AND's in the same query.

Suggestions?

 

 

It isn't. Ever heard of operator precedence?

Is 1 + 2 + 3 * 4 same as (1 + 2 +3 ) * 4 ?

 

Think of OR as of  + and AND as *

 

SELECT stn,
tdate
FROM NS_data
WHERE (stn LIKE '780%'
OR stn LIKE '403%'
OR stn LIKE '587%')
AND tdate = '$date'

It isn't. Ever heard of operator precedence?

Is 1 + 2 + 3 * 4 same as (1 + 2 +3 ) * 4 ?

 

Think of OR as of  + and AND as *

 

SELECT stn,
tdate
FROM NS_data
WHERE (stn LIKE '780%'
OR stn LIKE '403%'
OR stn LIKE '587%')
AND tdate = '$date'

 

Great stuff, thanks Mchl! 

 

I haven't heard of operator precedence before but i'm about to do some reading on it.  Thanks for the info :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.