MySQL and LIKE comparison with % - mysql

If someone passes a '%' to a field that compares in my sql with su.username LIKE CONCAT('%', email ,'%')) it returns all rows. It ends up looking like su.username LIKE CONCAT('%%%'). Can I get around this in anyway without filtering out the '%'?

I'm assuming you mean you want to escape the % so it matches a literal % instead of anything.
In that case, you just need:
... su.username LIKE CONCAT('%',REPLACE(email,'%','\\%'),'%')

You need to escape the %, so it literally matches '%'
select * from mytable
where mycol like '%\%%';

Related

How do I select all(*) in a row where a column eg. PHONE ends with certain intergers? [duplicate]

I have a column that contains strings as:
aaa_1
aaa_11
I need to query strings that ends with _1. I tried the following:
select col from table where col like %_1;
But the query gives me the strings that ends with _1 and _11. How can I correct this ?
Try this:
select col from table where col like '%\_1'
character _ is a jolly, like %, but it matches only a single character, so you have to escape it with \
See here: http://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html#operator_like
You'll want to use something more substring related.
Try a where clause like:
SELECT col from table WHERE RIGHT(col, 2) = '_1'
You should escape % and _ by adding backslash \ as they are wildcards in mysql:
http://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html
String | Description
\% | Matches one “%” character
\_ | Matches one “_” character
Many of these will pick up things like %_12 or %_111121, etc. Make sure you test out various cases to be sure these SELECT statements are giving you the proper subset.
SELECT col FROM table WHERE col REGEXP '_1[[:>:]]'
You can make a quick use of this query to filter out strings ending with specific character(s).
The below query output will give all names ending with 'er'.
select column_name
from table
where column_name regexp '.*er$';

MySQL - need to find records without a period in them

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 '[\\.]';

like clause doesn't work as it said in MySQL SQL

I have a table, such as
create table table1(
name varchar(32),
);
And there's some data in it. When I select like this:
select * from table1 where name like 'Jack2%';
there will be Jack2.
But if I select like this:
select * from table1 where name like 'Jack[0-9]%';
there will be nothing;
And I also tried regexp to subsitute like, but it also didn't work!
What's wrong?
You've confused two different pattern-matching mechanisms. SQL LIKE uses % to match anything and _ to match any single character; it does not have anything like [0-9] to match a digit. That looks like a character class from a regular expression.
Standard SQL has no support for regular expressions at all, but MySQL does - you just have to use RLIKE (or REGEXP, but that doesn't read as nicely IMO) instead of LIKE. But that means that you have to replace the % with the regular-expression equivalent .*, too.
SELECT * FROM table1 WHERE name RLIKE 'Jack[0-9].*';
Fiddle
MySQL REGEX
select * from Table1 where `name` REGEXP 'Jack[0-9]'
You can use RLIKE instead
SELECT * FROM table1 WHERE name RLIKE 'Jack[0-9].*';
And please note the the '%' operator won't work with RLIKE, you have to use a regular expression pattern like '.*' instead.

Query to select strings end with certain character

I have a column that contains strings as:
aaa_1
aaa_11
I need to query strings that ends with _1. I tried the following:
select col from table where col like %_1;
But the query gives me the strings that ends with _1 and _11. How can I correct this ?
Try this:
select col from table where col like '%\_1'
character _ is a jolly, like %, but it matches only a single character, so you have to escape it with \
See here: http://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html#operator_like
You'll want to use something more substring related.
Try a where clause like:
SELECT col from table WHERE RIGHT(col, 2) = '_1'
You should escape % and _ by adding backslash \ as they are wildcards in mysql:
http://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html
String | Description
\% | Matches one “%” character
\_ | Matches one “_” character
Many of these will pick up things like %_12 or %_111121, etc. Make sure you test out various cases to be sure these SELECT statements are giving you the proper subset.
SELECT col FROM table WHERE col REGEXP '_1[[:>:]]'
You can make a quick use of this query to filter out strings ending with specific character(s).
The below query output will give all names ending with 'er'.
select column_name
from table
where column_name regexp '.*er$';

Search for a multiple percent signs

I'm trying to find '%%' string in content.
SELECT * FROM myTable WHERE myColumn LIKE '%\%\%%'
But it returns also the rows, where only one percent sign found.
How to find only the rows, where two percent signs exists?
Thanks.
you can also use REGEXP for this,
SELECT *
FROM table1
WHERE str REGEXP '%%'
SQLFiddle Demo
If you are indeed only looking for literal % and don't need the wildcards, this would be made simpler using LOCATE(), where the % will be parsed as literals requiring no escaping.
SELECT * FROM myTable WHERE LOCATE('%%', myColumn) > 0;