I'm new to mysql so maybe my question will sound very simple but if some body can help with this:
I need to search 2 columns and output 2 exact columns that have exact string,
for example
column1 column2
sun sun
the problem with this code is that it find this column values too.
column1 column 2
sun sun
somesun sun
this is the code
SELECT tags.string, tag
FROM tags
INNER JOIN tastings
ON tastings.tags LIKE CONCAT('%', tags.string, '%')
the '%' sandwiching don't help and when I remove one of them I don't get any result.
for example:
ON tastings.tags LIKE CONCAT(tags.string, '%')
this code also don't give any results
ON tastings.tags = tags.string
I was wondering why this work good when I sandwiching it with '%', so some spaces must to be. then to check it I used trim() function. and it worked :)
what was confusing is that CSV file don't have any spaces
so this is the final code that work:
SELECT tags.string, tag
FROM tags
INNER JOIN tastings
ON tastings.tags = trim(tags.string)
Thanks to all who tried to help :)
Related
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) > -
I've been to the regexp page on the MySQL website and am having trouble getting the query right. I have a list of links and I want to find invalid links that do not contain a period. Here's my code that doesn't work:
select * from `links` where (url REGEXP '[^\\.]')
It's returning all rows in the entire database. I just want it to show me the rows where 'url' doesn't contain a period. Thanks for your help!
SELECT c1 FROM t1 WHERE c1 NOT LIKE '%.%'
Your regexp matches anything that contains a character that isn't a period. So if it contains foo.bar, the regexp matches the f and succeeds. You can do:
WHERE url REGEXP '^[^.]*$'
The anchors and repetition operator make this check that every character is not a period. Or you can do:
WHERE LOCATE(url, '.') = 0
BTW, you don't need to escape . when it's inside [] in a regexp.
Using regexp seems like an overkill here. A simple like operator would do the trick:
SELECT * FROM `links` WHERE url NOT LIKE '%.%
EDIT:
Having said that, if you really want to negate regexp, just use not regexp:
SELECT * FROM `links` WHERE url NOT REGEXP '[\\.]';
I have table 'key' with rows
happy new year
I love NY
I have table 'content' with rows
I want to say you: happy new year, Mike
I saw that banner with I love NY really
I would like to find in table 'content' words from table 'key' and replace it with hrefs. The table 'content' will be like
I want to say you: happy new year, Mike
I saw that banner with I love NY really
Is it possible to make it using mysql syntax?
You can get pretty close with this:
update content c join
keys k
on c.col like concat('%', k.col, '%')
set c.col = replace(c.col, k.col,
concat('<a href="', replace(k.col, ' ', '-'), '">',
k.col, '</a>')
);
The way that update works with multiple matches is that only one of the matches takes effect. So, this will only replace one key value. But, it will do it throughout the entire string. In other words, if the same key appears multiple times, then it will be replaced each time.
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_%";