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$'
Related
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
I want to write a query that title start with A or B
is this correct?
I dont want use OR
I want use it in mysql ,
select * from table where title like `[AB]%`
Use REGEXP instead of LIKE:
SELECT * FROM table
WHERE title REGEXP '^[AB]'
DEMO
Or just use a substring:
SELECT * FROM table
WHERE LEFT(title, 1) IN ('A', 'B');
you can do it like
select * from table where title like `A%` OR title like `B%`
another way is to use regular expression
select * from table where title REGEXP '^(A|B)';
This is correct way:
SELECT * FROM table WHERE title LIKE 'A%' or title LIKE 'B%';
What you wrote should work. or you can use,
select *
from table_name
where title LIKE 'A%' OR title LIKE 'B%'
Unfortunately, the LIKE operator in SQL only supports a limited syntax. It doesn't support a regex style syntax.
You have to do divide it into two expressions:
select * from table where (title like 'A%' or title like 'B%')
Note:
In this case the parenthesis are superfluous, but since OR has a lower precedence than AND I think it is good practice to routinely add parenthesis around ORexpressions in SQL.
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$)';
I have a requirement to retrieve the records like "Name19","Name21","Name56", these kind of records in Mysql.Please help how to get it.
SELECT * FROM YourTable WHERE YourColumn regexp '^Name[0-9]+'
Try This
SELECT * FROM <NameTable> WHERE <Name> LIKE '%[0-9]'
I have this MySQL query.
I have database fields with this contents
sports,shopping,pool,pc,games
shopping,pool,pc,games
sports,pub,swimming, pool, pc, games
Why does this like query does not work?
I need the fields with either sports or pub or both?
SELECT * FROM table WHERE interests LIKE ('%sports%', '%pub%')
Faster way of doing this:
WHERE interests LIKE '%sports%' OR interests LIKE '%pub%'
is this:
WHERE interests REGEXP 'sports|pub'
Found this solution here: http://forums.mysql.com/read.php?10,392332,392950#msg-392950
More about REGEXP here: http://www.tutorialspoint.com/mysql/mysql-regexps.htm
The (a,b,c) list only works with in. For like, you have to use or:
WHERE interests LIKE '%sports%' OR interests LIKE '%pub%'
Why not you try REGEXP. Try it like this:
SELECT * FROM table WHERE interests REGEXP 'sports|pub'
You can also use REGEXP's synonym RLIKE as well.
For example:
SELECT *
FROM TABLE_NAME
WHERE COLNAME RLIKE 'REGEX1|REGEX2|REGEX3'
Don't forget to use parenthesis if you use this function after an AND parameter
Like this:
WHERE id=123 and(interests LIKE '%sports%' OR interests LIKE '%pub%')
Or if you need to match only the beginning of words:
WHERE interests LIKE 'sports%' OR interests LIKE 'pub%'
you can use the regexp caret matches:
WHERE interests REGEXP '^sports|^pub'
https://www.regular-expressions.info/anchors.html
Your query should be SELECT * FROM `table` WHERE find_in_set(interests, "sports,pub")>0
What I understand is that you store the interests in one field of your table, which is a misconception. You should definitively have an "interest" table.
Like #Alexis Dufrenoy proposed, the query could be:
SELECT * FROM `table` WHERE find_in_set('sports', interests)>0 OR find_in_set('pub', interests)>0
More information in the manual.
More work examples:
SELECT COUNT(email) as count FROM table1 t1
JOIN (
SELECT company_domains as emailext FROM table2 WHERE company = 'DELL'
) t2
ON t1.email LIKE CONCAT('%', emailext) WHERE t1.event='PC Global Conference';
Task was count participants at an event(s) with filter if email extension equal to multiple company domains.