Is there a better way of doing the below sql query I am not sure if the Like statement is the best option as the location column only contains exact matches.
INSERT INTO test_reports (Table_Name, Total_Count)
SELECT "table1", COUNT(1)
FROM table1
WHERE location LIKE 'birmingham'
There's no wildcards in your search string. Without wildcards, like is exactly the same as =.
Related
I'm new to SQL and I'm stuck on a multiple find in SQL.
Here I'm trying to search table1 in the three columns. But I'm stuch in trying to find two phrases. I'm trying with an OR statement.
If I remove OR LIKE '%paris%' it works but how do I find multiple words/phrases. And would this statement be case sensitive?
I'm also using MySQL to run the above.
SELECT * FROM `table1`
WHERE
CONCAT_WS('|',`target_1`,`target_2`,`target_3`)
LIKE '%london%' OR LIKE '%paris%'
In your code your second condition is sintactically wrong because is missing the a part for the match
so you should repeat the condition as
SELECT *
FROM `table1`
WHERE CONCAT_WS('|',`target_1`,`target_2`,`target_3`) LIKE '%london%'
OR CONCAT_WS('|',`target_1`,`target_2`,`target_3`) LIKE '%paris%'
One option is to use regular expression, then you can also have case insensitive matching (3rd parameter to REGEXP_LIKE)
SELECT *
FROM table1
WHERE REGEXP_LIKE(CONCAT_WS('|',`target_1`,`target_2`,`target_3`), 'london|paris', 'i');
You should repeat the left operand and use (maybe?) multiple conditions i/o concatenating:
SELECT * FROM `table1`
WHERE (`target_1` like '%london%' OR `target_1` like '%paris%')
AND (`target_2` like '%london%' OR `target_2` like '%paris%')
AND (`target_3` like '%london%' OR `target_3` like '%paris%')
I'm trying to write a mysql query to detect three or more periods in a email string. Example
gr.em.test.t#domain.com
I'm thinking I could use some sort of mysql like query, but I'm not sure how to best write the regex. Any thoughts?
SELECT MyColumn
From MyTable
WHERE MyColumn like
No need for any type of regular expression, simply use the LIKE clause like you have, so you're on the right track. The following query should give you what you want:
SELECT MyColumn FROM MyTable WHERE MyColumn LIKE "%.%.%.%";
I've a problem and I'm guessing if there's a better way to achieve my goal. I've this query in Mysql to retrieve some rows:
SELECT *
FROM table
WHERE field IN ('V','S','G','B')
What I would like to do is to run a query that retrieve the rows where the field has value LIKE those in the IN list. I know that the trivial way is to use this query:
SELECT *
FROM table
WHERE field LIKE '%V%' OR field LIKE '%S%' OR
field LIKE '%G%' OR field LIKE '%B%'
What I want to know is there's an operator that do this or at least, if that operator does not exist, a better way to write the query.
Thanks to everyone that will help me.
Put the values in a table (Params) then use
SELECT *
FROM table
WHERE EXISTS (
SELECT *
FROM Params
WHERE table.field LIKE '%' + Params.col + '%'
);
Consider also putting the wildcard characters into the values in the Params table.
I am trying to select a field based on it meeting one of 3 criteria... and I'm not sure how to do this. I think a RegExp is probably the best method buy I'm unfamiliar with writing them.
Say I have the integer 123, I would like to match the following cases:
123 (thats 123 only with no spaces or other numbers after it)
123-10/12/2007 00:00 (thats 123 with a hyphen and a date, or actually it could be anything after the hyphen)
123_1014859 (thats 123 with an underscore, or again anything after the underscore)
Is there a way to do this using MySQL?
A regex is plausible, but it's not the best performing option. The last comparison put MySQL's regex support as being par with wildcarding the left side of a LIKE statement -- works, but the slowest of every option available.
Based on your example, you could use:
SELECT t.*
FROM YOUR_TABLE t
WHERE t.column LIKE '123-%'
OR t.column LIKE '123_%'
Another alternative, because OR can be a performance issue too, would be to use a UNION:
SELECT a.*
FROM YOUR_TABLE a
WHERE a.column LIKE '123-%'
UNION ALL
SELECT b.*
FROM YOUR_TABLE b
WHERE b.column LIKE '123_%'
UNION ALL will return all results from both tables; UNION removes duplicates, and is slower than UNION ALL for that fact.
select * from foo where bar regexp '^123-|_'
(not tested)
I would avoid using regex inside a SQL statement. Someone can correct me if I am wrong, but MySQL has to use another engine to run the regex.
SELECT * FROM table
WHERE field like "123"
OR field LIKE "123-%"
OR field like "123_%";
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$'