2

I have an array:

$codes = array (97121, 97122, 97123, 97180);

The real array is much longer, it contains nearly 130 codes so I think it doesn't have to be a loop.

And I have a mySQL table that has a column VilleAnn (city with postal code) ex:

  • Sainte-Anne 97180
  • Sanary-sur-Mer 83110

Using mysqli I have to select where VilleAnn doesn't contain any postal code from the array $codes.

If the VilleAnn is a simple postal code I would say:

SELECT * FROM table WHERE VilleAnn NOT IN(97121, 97122, 97123, 97180)

But in this case it must be something using NOT LIKE "%..."

Any suggestions?

medk
  • 9,233
  • 18
  • 57
  • 79

4 Answers4

3

You could you REGEXP:

SELECT * FROM table WHERE VilleAnn NOT REGEXP '97121|97122|97123|97180|....'
Gurwinder Singh
  • 38,557
  • 6
  • 51
  • 76
2

Something like this:

SELECT * 
FROM `table` 
WHERE (
    `VilleAnn` NOT LIKE '%97121%' AND 
    `VilleAnn` NOT LIKE '%97122%' AND 
    `VilleAnn` NOT LIKE '%97123%' AND 
    `VilleAnn` NOT LIKE '%97180%'
)
Ron van der Heijden
  • 14,803
  • 7
  • 58
  • 82
2

In condition doesn't work with the wildcard characters. Your best bet is to try and extract the postcode from the original field, in this example

SELECT * FROM table WHERE right(VilleAnn,5) NOT IN ('97121', '97122', '97123', '97180')

I presume real life is more complicated, so this might need to be adjusted to reflect the actual format of the field.

NotCaring
  • 78
  • 7
1
SELECT * FROM table WHERE VilleAnn NOT REGEXP '[0-9]';

This will help you.

For to check four times occurrences you can use:

SELECT * FROM table WHERE VilleAnn NOT REGEXP '[0-9]{4}';
Nidhi257
  • 754
  • 1
  • 5
  • 23