It is possibile to search for rows that does not contain a white space in it?
I have try this but return an error
SELECT * FROM `users` WHERE `name`
NOT LIKE '% %'
in mysql 8 and newer you can use REGEXP_LIKE for more complicated expression too.
SELECT *
FROM users
WHERE REGEXP_LIKE(name, '[[:blank]]')
REGEXP_LIKE
Related
I want to find usernames with reduplicated words in my database
Below is my SQL statement:
SELECT * FROM users WHERE BINARY `name` regexp 'a{2}|b{2}|c{2}|d{2}|e{2}|f{2}|g{2}|h{2}|i{2}|j{2}|k{2}|l{2}|m{2}|n{2}|o{2}|p{2}|q{2}|r{2}|s{2}|t{2}|u{2}|v{2}|w{2}|x{2}|y{2}|z{2}'
I guess there should be an easier way, but I don't know how to do it.
I also want to find usernames like 'ABAA' or 'AABA', but I won't write this SQL or say this regex
As MySQL's regex engine does not support backreferences, your approach is reasonable. To simplify your query, you could maintain a table of double letters:
table: letters (val)
aa
bb
...
zz
Now your query can be simplified to:
SELECT *
FROM users u
WHERE EXISTS (
SELECT 1
FROM letters l
WHERS INSTR(u.name, l.val) > 0
);
Try:
create table users(name varchar(100));
insert into users (name) values
("abaa"),
("bba"),
("abc"),
("aCCa");
SELECT * FROM users WHERE LOWER(name) REGEXP 'aa|bb|cc|dd|ee|ff|gg|hh|ii|jj|kk|ll|mm|nn|oo|pp|qq|rr|ss|tt|uu|vv|ww|xx|yy|zz';
Prints:
abaa
bba
aCCa
i think i found the answer,I upgraded my MySQL database to MariaDB.
this is my sql statement:
SELECT * FROM users WHERE `name` REGEXP '([a-z])\\1'
This is the sql statement to query like 'aaba':
SELECT * FROM users WHERE `name` REGEXP '([a-z])\\1((?!\\1)[a-z])\\1'
Note: Because MariaDB uses the C escape syntax in strings (for example, "\n" to represent the newline character), you must double any "" that you use in your REGEXP strings.
In MySQL 8.0
WHERE name REGEXP '([a-z])\\1'
Let the collation take care of ignore case.
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 '[\\.]';
After a database import, there's some rows with dirty fields that end in (sometimes multiple) spaces saved in the database, and in the interest of finding them amidst the many thousands of other rows, I'm trying to do a query like:
SELECT * FROM `mytable` WHERE `dirtyfield` REGEXP ' $'
But that returns zero rows. I tried a few other variations with other results:
SELECT * FROM `mytable` WHERE `dirtyfield` REGEXP '[[:space:]]$' -- Zero Rows
SELECT * FROM `mytable` WHERE `dirtyfield` REGEXP '[[.space.]]$' -- Zero Rows
SELECT * FROM `mytable` WHERE `dirtyfield` REGEXP '[[.space.]]' -- Those with a space anywhere in the value
SELECT * FROM `mytable` WHERE `dirtyfield` REGEXP '[[.space.]]{2}' -- Those with two spaces in a row
Finding all with a single space doesn't help much, since some clean rows have single spaces between words in that field. That last one catches 90% of the dirty rows, but misses those that have just a single space at the end. Is there something wrong with how I'm using the $ symbol to indicate the end of a field?
The MySQL RIGHT() and SUBSTRING() functions seem to strip off whitespace when calculating the end of the field:
SELECT * FROM `mytable` WHERE RIGHT(`dirtyfield`)=" " -- Only returns one row that has " " for that field
SELECT * FROM `mytable` WHERE SUBSTR(`dirtyfield`,-1)=" " -- Only returns one row that has " " for that field
One other try using a comparison didn't work either:
SELECT * FROM `mytable` WHERE TRIM(`dirtyfield`)!=`dirtyfield` -- zero rows returned
The "dirtyfield" field is a VARCHAR(128), if that matters.
EDIT: I'm an idiot; the fields don't end in spaces, then end in multiple spaces followed by a comma (imported from a bad CSV file).
SELECT * FROM `mytable` WHERE RIGHT(`dirtyfield`,1)=','
That query found them. I was looking at the output of the tables in a comma-separated view and didn't notice the commas were doubled-up.
Had a similar problem. The compare you made with trim():
SELECT * FROM `mytable` WHERE TRIM(`dirtyfield`)!=`dirtyfield`
doesnt work, but:
SELECT * FROM mytable where char_length(dirtyfield) > char_length(trim(dirtyfield))
gets the work done and shows you the rows that have spaces both at the start and/or end of the content. The character count works. Quite honestly i don't know why trim() doesn't compare directly on the first query.
Hope this helps. Like your field, this solution is admittedly a bit dirty.
I think this may be correct:
SELECT * FROM `mytable` WHERE `dirtyfield` REGEXP '[[.space.]]+$'
It matches field ending ($) with 1 or more (+) spaces ([[.space.]])
Anyway if you want to do the same thing with LIKE statement is easier with:
... LIKE '% '
as in answer below.
I have not tried this but I think this might work to find entries that ends with a blank space
SELECT * FROM `mytable` WHERE `dirtyfield` LIKE "% "
Hey i was doing something similar and got in this page, Check below query, this might help..
SELECT * FROM `mytable` WHERE `dirtyfield` LIKE "% %"
Have you considered that you actually get rows with spaces in it, but the web browser doesn't render them for you?
The value " two (2 or more spaces) words " looks like "two words" in your browser.
Try a:
WHERE dirtyfield REGEXP '(^[[:space:]]|[[:space:]]{2,}|[[:space:]]$)'
This should fetch all of those rows. Just consider that you might get the right rows, but it doesn't look that way due to browser.
The field table.name contains 'Stylus Photo 2100' and with the following query
SELECT `name` FROM `table` WHERE `name` LIKE '%Stylus 2100%'
I get no results. Of course i would if i searched
SELECT `name` FROM `table` WHERE `name` LIKE '%Photo 2100%'
How can I select the record by searching 'Stylus 2100' ?
Thanks
Well if you know the order of your words.. you can use:
SELECT `name` FROM `table` WHERE `name` REGEXP 'Stylus.+2100'
Also you can use:
SELECT `name` FROM `table` WHERE `name` LIKE '%Stylus%' AND `name` LIKE '%2100%'
I think that the best solution would be to use Regular expressions. It's cleanest and probably the most effective. Regular Expressions are supported in all commonly used DB engines.
In MySql there is RLIKE operator so your query would be something like:
SELECT * FROM buckets WHERE bucketname RLIKE 'Stylus|2100'
I'm not very strong in regexp so I hope the expression is ok.
Edit
The RegExp should rather be:
SELECT * FROM buckets WHERE bucketname RLIKE '(?=.*Stylus)(?=.*2100)'
More on MySql regexp support:
http://dev.mysql.com/doc/refman/5.1/en/regexp.html#operator_regexp
You can just replace each space with %
SELECT `name` FROM `table` WHERE `name` LIKE '%Stylus%2100%'
The correct solution is a FullText Search (if you can use it) https://dev.mysql.com/doc/refman/5.1/en/fulltext-search.html
This nearly does what you want:
SELECT * FROM buckets WHERE bucketname RLIKE '(Stylus|2100)+.*(Stylus|2100)+';
SELECT * FROM buckets WHERE bucketname RLIKE '(Stylus|2100|photo)+.*(Stylus|2100|photo)+.*(Stylus|2100|photo)+.*';
But this will also match "210021002100" which is not great.
you need to do something like this,
SELECT * FROM buckets WHERE bucketname RLIKE 'Stylus.*2100';
or
SELECT * FROM buckets WHERE bucketname RLIKE '(Stylus)+.*(2100)+';
Assuming that your search is stylus photo 2100. Try the following example is using RLIKE.
SELECT * FROM `buckets` WHERE `bucketname` RLIKE REPLACE('stylus photo 2100', ' ', '+.*');
EDIT
Another way is to use FULLTEXT index on bucketname and MATCH ... AGAINST syntax in your SELECT statement. So to re-write the above example...
SELECT * FROM `buckets` WHERE MATCH(`bucketname`) AGAINST (REPLACE('stylus photo 2100', ' ', ','));
SELECT `name` FROM `table` WHERE `name` LIKE '%Stylus % 2100%'
I'm using this query to select data:
mysql_query("SELECT * FROM products WHERE product_name LIKE '%".$search."%'");
The only problem is, that it sometimes selects more, than I would like.
For example, I would like to select product "BLA", but my query select product "BLABLA" as well. To be clear, if i wanted to select "Product 1", I don't want the query to select "Product 11".
Does anybody know how to manage that?
Thanks.
Do you just want to search on word boundaries? If so a crude version might be:
SELECT * FROM products WHERE product_name LIKE "% foo %";
Or you could be a bit cleverer and look for word boundaries with the following REGEXP
SELECT * FROM products WHERE product_name RLIKE "[[:<:]]foo[[:>:]]";
Found this question on Google, so I figure that some people may still stumble upon this so here's my pretty inelegant attempt:
SELECT * FROM products
WHERE product_name LIKE 'BLA %' #First word proceeded by more words
OR WHERE product_name LIKE '% BLA' #Last word preceded by other words
OR WHERE product_name LIKE '% BLA %' #Word in between other words
OR WHERE product_name = 'BLA'; #Just the word itself
Not sure about the efficiency or if this covers all cases, so feel free to downvote if this is really inefficient or too inelegant.
Try using regular expressions:
SELECT
*
FROM
`products`
WHERE
product_name regexp '(^|[[:space:]])BLA([[:space:]]|$)';
SELECT *
FROM products
WHERE product_name = 'BLA'
will select exact BLA
SELECT *
FROM products
WHERE product_name LIKE 'BLA%'
will select BLADDER and BLACKBERRY but not REBLAND
To select BLA as the first word of the string, use:
SELECT *
FROM products
WHERE product_name RLIKE '^Bla[[:>::]]'
AND product_name LIKE 'Bla%'
The second condition may improve your query performance if you have an index on product_name.
Remove LIKE keyword and use = for exact match
EDIT
do not forgot to escape user input using mysql_real_escape_string otherwise your query will fail if some one enter quotes inside the input box.
$search=mysql_real_escape_string($search);
mysql_query("SELECT * FROM products WHERE product_name='".$search."'");
Then don't use LIKE, but search for equality.
ie.
mysql_query("SELECT * FROM products WHERE product_name = '".$search."'");
BTW I hope you sanitize/escape $search before using it in a query.
You can just cover all the possible options.
SELECT
*
FROM
`products`
WHERE
`product_name` like 'BLA' -- Column cointains just that word
OR `product_name` like 'BLA %' -- The word appears at the beginning
OR `product_name` like '% BLA' -- The word appears at the end
OR `product_name` like '% BLA %'; -- The word appears in the middle
Use equals (=)?
mysql_query("SELECT * FROM products WHERE product_name = '".$search."'");
If you are looking to match EXACT words don't use LIKE.
EDIT: That clears things up a bit then. Just add a space after the search term. Or even add the hyphen (-) if that is always in the search term.
mysql_query("SELECT * FROM products WHERE product_name LIKE '".$search." -%'");
try to use regular expression in query
mysql_query("SELECT * FROM products WHERE product_name regexp '".$search."'");
If you want to search exact word matching from MySql using LIKE then you use:
SELECT * FROM tableName WHERE columnName LIKE 'your_query' ;
In Laravel Eloquent you can do this like below.
Category::where('name', 'RLIKE ', "[[:<:]]"$words"[[:>:]]");
In raw query, you can search it like this.
SELECT * FROM categories WHERE name RLIKE "[[:<:]]categoryNameHere[[:>:]]";
you can use select query like this ,i also use in cakePHP and it's helpful.
Select * from `users` where username COLLATE latin1_general_cs LIKE '%$email%'