I need to get the data from a table where the row values are comma-separated strings, like this: 5,10,16,25,7 I'm also using a LEFT JOIN, so I need something like this:
// ...
SELECT ... s.`other_thing`
LEFT JOIN `something` s
ON w.`whatever` = REGEXP CONCAT('(,|^)', s.`id`, '(,|$)')
// ...
I need to get something like this: (,|^)5(,|$) on ON
EDIT: I solved this with a simple LIKE CONCAT('%', s.id, '%')
EDIT 2: If you want to concat the Regex, you can use: REGEXP CONCAT('(^|,)(',s.id,')(,|$)')
I strongly discourage this data model. Your next question is likely to be about performance -- and there is really no hope. You should have a junction/association table for the lists, rather than storing multiple values in a string.
Sometimes, we are stuck with other people's really, really, really bad design decisions. If this is the case, MySQL has a function to help:
SELECT ... s.`other_thing`
FROM x LEFT JOIN
something s
ON find_in_set(s.id, x.really_bad_list_format) > -
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 want to convert this:
SELECT id,songTitle,artistName, trackId
FROM songs
WHERE (songTitle, artistName) IN (('come together', 'the beatles'),('all the small things', 'blink-182'))
To something like this but I don't know the right syntax:
SELECT id,songTitle,artistName, trackId
FROM songs
WHERE (songTitle, artistName) IN LIKE (('%come together%', '%the beatles%'),('%all the small things%', '%blink-182%'))
Except I'm searching 100s of more songs at once. We could use REGEXP too I just don't know the right syntax for either of those.
WHERE (a,b) IN ((1,2), ...) is very poorly optimized.
Leading wild cards in LIKE prevents use of an index.
You can't do the construct you attempted.
So, performance aside, let's look at how to perform the task:
WHERE ( songTitle LIKE '%come together%' AND artistName LIKE '%the beatles%')
OR ( .... )
OR ...
Sorry, there is no short cut.
REGEXP can't help in this case.
FULLTEXT indexing is something to consider, but I don't see that it would help in this example.
I have created a subquery that searches for a particular string from one table, using the SQL LIKE condition. I would like to use this subquery's result as the string to search for in my main SQL query also using the LIKE condition. I tried the below code but I get syntax errors, although it seems to be the way it should be done...sadly I am not an SQL expert and just trying to feel this out.
SELECT * FROM `allcesseries`
WHERE series_id LIKE '%'+(SELECT industry_code FROM `ceindustry` WHERE industry_name LIKE '%Technical and trade schools%')+'%'
SELECT * FROM `allcesseries`
WHERE series_id LIKE concat('%',
(SELECT industry_code FROM `ceindustry`
WHERE industry_name LIKE '%Technical and trade schools%'),
'%')
I would suggest that you use exists in this case:
SELECT *
FROM `allcesseries` a
WHERE EXISTS (SELECT 1
FROM `ceindustry` c
WHERE c.industry_name LIKE '%Technical and trade schools%' AND
a.series_id LIKE CONCAT('%', c.industry_code, '%')
);
If you have multiple matches, then this will work as expected.
You can also phrase this directly as a join, if you want:
SELECT a.*
FROM `allcesseries` a JOIN
ceindustry c
ON c.industry_name LIKE '%Technical and trade schools%' AND
a.series_id LIKE CONCAT('%', c.industry_code, '%')
But if there are multiple rows that satisfy the conditions in ceindustry, you will get duplicates.
I use a string for store the days of the week, something like this:
MTWTFSS. And if I search for MF (Monday and Friday) then the query must return all the strings that contain MF (for example: MWF, MTWTFS, MF, and so on).
I don't know how to do this in SQL (MySQL).
use LIKE with %-wildcard between the single characters:
SELECT * FROM table WHERE column LIKE '%M%F%';
note that this will only work if the characters are in correct order - searching for FM instead of MF won't give you any result.
you'll also need to find a way to insert the %s to your search-term, but taht shouldn't be a big problem (sadly you havn't said wich programming-language you're using).
if the characters can be in random order, you'll have to built a query like:
SELECT * FROM table WHERE
column LIKE '%M%'
AND
column LIKE '%F%'
[more ANDs per character];
SELECT * FROM yourTable WHERE columnName LIKE '%MF%'
Learn more:
http://www.sqllike.com/
Can you not just say
SELECT * FROM blah WHERE weekday LIKE "%MF%"
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_%";