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
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?

 

 

Link to comment
Share on other sites

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'

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.