Zergman Posted April 27, 2010 Share Posted April 27, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/199880-using-wildcards-in-mysql-in-statement/ Share on other sites More sharing options...
trq Posted April 27, 2010 Share Posted April 27, 2010 I believe you'll need to use multiple LIKEs. WHERE stn LIKE '780%' || stn LIKE '403%' || stn LIKE '587%' Quote Link to comment https://forums.phpfreaks.com/topic/199880-using-wildcards-in-mysql-in-statement/#findComment-1049135 Share on other sites More sharing options...
Mchl Posted April 27, 2010 Share Posted April 27, 2010 You can't. Use LIKE WHERE stn LIKE '780%' OR stn LIKE '403%' OR stn LIKE '587%' Quote Link to comment https://forums.phpfreaks.com/topic/199880-using-wildcards-in-mysql-in-statement/#findComment-1049136 Share on other sites More sharing options...
fenway Posted April 28, 2010 Share Posted April 28, 2010 In fact, IN() is just shorthand for the optimizer, and just get re-written as ORs -- which is also why you can't use NULL in there, which came up just recently. Quote Link to comment https://forums.phpfreaks.com/topic/199880-using-wildcards-in-mysql-in-statement/#findComment-1049708 Share on other sites More sharing options...
Zergman Posted April 29, 2010 Author Share Posted April 29, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/199880-using-wildcards-in-mysql-in-statement/#findComment-1050789 Share on other sites More sharing options...
Zergman Posted April 30, 2010 Author Share Posted April 30, 2010 I think I got it. Did this and it seems to work SELECT stn, tdate FROM NS_data WHERE stn LIKE '780%' AND tdate = '2010-04-29' OR stn LIKE '403%' AND tdate = '2010-04-29' Is this the right way to do it? Quote Link to comment https://forums.phpfreaks.com/topic/199880-using-wildcards-in-mysql-in-statement/#findComment-1050806 Share on other sites More sharing options...
Mchl Posted April 30, 2010 Share Posted April 30, 2010 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' Quote Link to comment https://forums.phpfreaks.com/topic/199880-using-wildcards-in-mysql-in-statement/#findComment-1050884 Share on other sites More sharing options...
Zergman Posted April 30, 2010 Author Share Posted April 30, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/199880-using-wildcards-in-mysql-in-statement/#findComment-1051298 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.