1

I've a tricky question. I have a table with numbers:

  • 37823782
  • 37823782
  • 37823782
  • 38478934
  • 90003922

And another table with prefixes:

  • 378
  • 3847
  • 384
  • 001

I want to find all numbers matching the longest prefix. I succeded with this code:

    $result = mysql_query("SELECT numbers FROM table1 GROUP BY numbers") or die ("Query error code 1"); 
while($row = mysql_fetch_array($result))
{

    $numbers =$row["numbers"];

    $result2 = mysql_query("SELECT * FROM table2 WHERE '".$numbers."' LIKE CONCAT(prefix, '%') ORDER BY CHAR_LENGTH(prefix) DESC LIMIT 1");
    while($row2 = mysql_fetch_array($result2))
    {

        // That's it

    }       

}

Now what i want to simply make the opposite thing. I want to find all numbers not matching any prefix. In short in the above example i made i should get "90003922". I thought to use NOT LIKE CONCAT (prefix, '%') but it's not working. Any idea?

Aldwoni
  • 1,168
  • 10
  • 24
user1274113
  • 436
  • 8
  • 21

2 Answers2

1

You can try this

SELECT * FROM table2 WHERE '".$numbers."' NOT LIKE 'prefix%'
Suresh Kamrushi
  • 15,627
  • 13
  • 75
  • 90
1

One-query solution will look like this. Try it

SELECT * FROM table1 LEFT JOIN table2 ON table1.numbers LIKE CONCAT(table2.prefix,'%') WHERE table2.prefix IS NULL
Alexander Taver
  • 474
  • 3
  • 4
  • Thank you for your answer but it works in a strange way. I can see even numbers matching prefixes. For example i can see: "349000000" even if in my table2.prefix there's "349". My table1.numbers is something like "whatever:3495541496@blabla" so maybe i should use **LIKE CONCAT(table2.prefix,':%@')** but it's all the same. – user1274113 Jan 06 '13 at 13:28
  • Please, update your post with some exact samples of your data in column `numbers`. I'll be able to fix my query – Alexander Taver Jan 06 '13 at 14:12