MySQL: Usage of 'REGEXP' instead of 'FIND_IN_SET' - mysql

I am having limitation in using MySQL's FIND_IN_SET function for searching array in set. Hence thinking of using of REGEXP. However can anyone help me constructing it.
E.g. My requirement
SELECT * FROM table AS t WHERE FIND_IN_SET('1,2,3', t.list);
Hence thinking of using REGEXP function to search array within set.
SELECT * FROM table AS t WHERE t.list REGEXP '1,2,3';
Can anyone help me building this REGEXP.

You can do like this:
SELECT * FROM table AS t WHERE t.list REGEXP '^9,|,9$|,9,' OR t.list =9

You can split your search string and continue to use FIND_IN_SET()
SELECT *
FROM `table` AS t
WHERE FIND_IN_SET('1', t.list)
AND FIND_IN_SET('2', t.list)
AND FIND_IN_SET('3', t.list)
Better yet normalize your data by introducing a many-to-many table.

For your requirements you can easily use:
SELECT *
FROM table1 AS t
WHERE t.list REGEXP '1|2|3';
To learn about regular expressions take look at this software:
http://www.weitz.de/regex-coach/

try the fallowing sql statement:
SELECT *
FROM table AS t
WHERE t.list REGEXP '^(1$|2$|3$)';

Related

SQL SELECT WHERE NOT LIKE

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

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 like to match pattern at end of string

I have the following strings in the following pattern in a table in my db:
this_is_my_string_tester1
this_is_my_string_mystring2
this_is_my_string_greatstring
I am trying to match all strings that start with a specific pattern split by underscores i.e.this_is_my_string_ and then a wildcard final section
Unfortunately there is an added complication where some strings like the following:
this_is_my_string_tester1_yet_more_text
this_is_my_string_mystring2_more_text
this_is_my_string_greatstring_more
Therefore taking the following as examples:
this_is_my_string_tester1
this_is_my_string_mystring2
this_is_my_string_greatstring
this_is_my_string_tester1_yet_more_text
this_is_my_string_mystring2_more_text
this_is_my_string_greatstring_more
I am trying to have returned:
this_is_my_string_tester1
this_is_my_string_mystring2
this_is_my_string_greatstring
I have no idea how to do this with a like statement. Is this possible if so how?
EDIT
There is one final complication:
this_is_my_string
needs to be supplied as a list i.e in
(this_is_my_string, this_is_my_amazing_string, this_is_another_amazing_string)
SELECT * FROM atable WHERE afield REGEXP 'this_is_my_string_[a-z]+'
It might be faster if you have an index on afield and do
SELECT * FROM atable WHERE afield REGEXP 'this_is_my_string_[a-z]+'
AND afield LIKE 'this_is_my_string_%'
After edit of question:
Either
SELECT * FROM atable
WHERE afield REGEXP '(this_is_my_string|this_is_my_amazing_string)_[a-z]+'
or maybe you want something like having a table with the prefixes:
SELECT *
FROM atable AS t,
prefixes AS p
WHERE afield REGEXP CONCAT(p.prefix, '_[a-z]+')
As by the reference documentation this should not be possible, as a pattern (string literal) is required. Give it a try nevertheless.
There the answer of #KayNelson with LIKE (?) and INSTR might do instead of REGEXP.
try this
SELECT * FROM db.table WHERE strings LIKE 'this_is_my_string_%' AND instr(Replace(strings,"this_is_my_string_",""),"_") = 0;
It checks if more _ occurs after replacing the standard "this_is_my_string_"

Search MySQL table for a kind of results

How can I search within a MySQL table for results ending in anything except ".jpg"?
Thanks.
You don't need to involve regular expressions, you can just do:
SELECT my_fields
FROM my_table
WHERE my_field NOT LIKE '%.jpg'
SELECT *
FROM mytable
WHERE myfield NOT RLIKE '\\.jpg$'