SQL SELECT WHERE NOT LIKE - mysql

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?

You could you REGEXP:
SELECT * FROM table WHERE VilleAnn NOT REGEXP '97121|97122|97123|97180|....'

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.

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%'
)

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}';

Related

MySQL - WHERE x IN ( column)

I tried something out. Here is a simple example in SQL Fiddle: Example
There is a column someNumbers (comma-seperated numbers) and I tried to get all the rows where this column contains a specific number. Problem is, the result only contains rows where someNumbers starts with the specific number.
The query SELECT * FROM myTable where 2 in ( someNumbers ) only returns the row with id 2 and not the row with id 1.
Any suggestions? Thank you all.
You are storing data in the wrong format! You should not be storing multiple values in a single string column. You should not be storing numbers as strings. Instead, you should have a junction table with one row per id and per number.
Sometimes, you just have no choice, because someone else created a really poorly designed database. For these situations, MySQL has the function find_in_set():
SELECT *
FROM myTable
WHERE find_in_set(2, someNumbers ) > 0;
The right solution, however, is to fix the data model.
While Gordon's answer is a good one, here is a way to do this with like
SELECT * FROM myTable where someNumbers like '2,%' or someNumbers like '%,2,%' or someNumbers like '%,2'
The first like checks if your array starts with the number you are looking for (2). The second one checks if 2 is within the array and the last like tests for appearance at the end.
Note that the commas are essential here, because something like '%2%' would also match ...,123,...
EDIT: As suggested by the OP it may happen that only a single value is present in the row. Consequently, the query must check this case by doing ... someNumbers = '2'
I would suggest this query :
SELECT * FROM myTable where someNumbers like '%2%'
It will select every entry where someNumbers contains '2'
Select * from table_name where coloumn_name IN(value,value,value)
you can use it

select data mysql

i have in my table places named field. there are space separated values(there are problem to store csv value in one field). now i want to fire query like below. how i can do ??
select * from tablename where variablename in places
i did try this way but it shows syntax error.
select * from tablename where variablename in replace(places,' ',',')
### places ###
bank finance point_of_interest establishment
Use FIND_IN_SET
For comma separated
SELECT *
FROM tablename
WHERE ( FIND_IN_SET( 'bank', variablename ) )
Refer : SQL Fiddle
For space separated
SELECT *
FROM tablename
WHERE ( FIND_IN_SET( 'bank', replace(variablename,' ',',') ) )
Refer : SQL Fiddle
The best solution would be to normalise your data structure and do not have a single field storing multiple values.
You can make a query work without normalisation, but any solutions would be lot less optimal from a performance point of view.
Use patter matching with like operator:
... where fieldname like '% searched_value %'
Use the replace() function and combine it with find_in_set():
... where find_in_set('searched_value',replace(fieldname,' ',','))>0
Hi I think your problem comes from the usage of IN
IN for MySql is used like this
SELECT *
FROM table_name
WHERE column_name IN (bank,finance,point_of_interest, establishment);
In case of you want to select places you need to specify each place into value like

write select query for the below requirement

This is my users table:
http://ezinfotec.com/Capture.PNG
I need to select all rows those are not contain 2 in except column. How to write a query for this using php & Mysql.
The result i expect for this query is only return last row only.
Thank you.
Don't store comma separated values in your table, it's very bad practice, nevertheless you can use FIND_IN_SET
SELECT
*
FROM
users
WHERE
NOT FIND_IN_SET('2', except)
Try this:
SELECT *
FROM users
WHERE CONCAT(',', except, ',') NOT LIKE '%,2,%'
this should work for you
SELECT *
FROM table
WHERE table.except NOT LIKE '%,2%'
OR table.except NOT LIKE '%2,%';

mysql search string and numbers like #654

I have a quick question how can I search #some number in mysql> For example
Select * from table where field contains #numbers?
where #numbers represent any number(example: #123, #345, ...) which has more then four characters. How should I do it correctly?
EDIT: I get what I need with this
Select * from table where field REGEXP '^#+[0-9]'
Is that ok? And I also need to be at least three characters long
If you need the numbers to be at least 3 digits long, try:
SELECT * FROM table WHERE field RLIKE '#[0-9]{3,}'
If I have good understood you that code should solve your problem : SELCET * FROM table WHERE field LIKE '%#%s'
SELECT * FROM table WHERE field RLIKE '#^[0-9]'
SELECT * FROM table WHERE fields REGEXP '^#[0-9]{3}'
If i understand you..
SELECT *
FROM table
WHERE field LIKE '%#[0-9][0-9][0-9]%'

How to find certain Hex values and Char() Values in a MySQL SELECT

Anyone know the syntax to search a MySQL table column by hex value or CHAR() value? Do I have to use a LIKE or IN()?
Something like:
SELECT * FROM `myWordTable` WHERE `definition` LIKE 0xE28098;
OR
SELECT * FROM `myWordTable` WHERE `definition` LIKE CHAR(128);
Thanks!
The second one is close.
Try this:
SELECT *
FROM `myWordTable`
WHERE `definition`
LIKE concat('%',CHAR(128),'%');
In the first example consider pattern match '%' == 0x25
SELECT *
FROM `myWordTable`
WHERE `definition`
LIKE 0x25E2809825;
You need to UNHEX() values you want to match on.
For example, finding all accented e's that were corrupted into 0xC3A9:
SELECT id, FirstName
FROM Person
WHERE FirstName
RLIKE UNHEX('c3a9');