In my database I have a record with title is "Học lập trình hướng đối tượng với Java"
When I search I use SELECT * FROM posts WHERE post_title like '%hoc%'
Or SELECT * FROM posts WHERE post_title like '%Học%'
But it doesn't work
How can I fix it?
The issue here is the character ọ in the database and the character ọ which you have used in the query are different even though they looks same.
The original character in the database is a combination of two characters, Latin small letter o (U+006F) and a Combining Dot Below ̣ (U+0323) character to form ọ. On the other hand, what you have used in the query is a single character, Latin Small Letter O With Dot Below ọ (U+1ECD).
You can fix the issue by using the same combination of characters as of the database:
SELECT * FROM posts WHERE post_title like '%Học%'
or you can use _ or % to skip unknown characters:
SELECT * FROM posts WHERE post_title like '%H__c%'
SELECT * FROM posts WHERE post_title like '%H%c%'
The like operator is case-sensitive, so if the case of the text you are searching for does not match the case of the text in the post_title column, the search will not return any results. You can use the LOWER() or UPPER() function to convert the text in the post_title column to lowercase or uppercase, respectively, before performing the search. For example:
select * from posts WHERE UPPER(post_title) like '%HỌC%';
OR
select * from posts WHERE LOWER(post_title) like '%học%';
OR
select * from posts WHERE post_title like '%Học%';
Working Demo: https://dbfiddle.uk/-fJrbLMk
Related
I've got a search function which creates query. My goal is to search for exact word, so if the phrase is 'hello' it should return only results with 'hello' (not with 'xhello', 'helloxx' etc). My code looks like:
SELECT (...) WHERE x RLIKE '[[:<:]]word[[:>:]]'
And it works for most of the cases, BUT
the problem starts when the phrase is f.e. '$hello', or 'helloŁ' etc - the special chars ruin the functionality.
Is there a way to handle it ?
Try
SELECT * FROM table WHERE x RLIKE '(^|[[:space:]])Hello([[:space:]]|$)'
or
SELECT * FROM table WHERE x RLIKE '(^| )Hello( |$)'
or
SELECT * FROM table WHERE x REGEXP '(^|[[:space:]])Hello([[:space:]]|$)'
or
SELECT * FROM test WHERE name REGEXP '(^| )Hello( |$)'
I'm trying to select all rows that contain only alphanumeric characters in MySQL using:
SELECT * FROM table WHERE column REGEXP '[A-Za-z0-9]';
However, it's returning all rows, regardless of the fact that they contain non-alphanumeric characters.
Try this code:
SELECT * FROM table WHERE column REGEXP '^[A-Za-z0-9]+$'
This makes sure that all characters match.
Your statement matches any string that contains a letter or digit anywhere, even if it contains other non-alphanumeric characters. Try this:
SELECT * FROM table WHERE column REGEXP '^[A-Za-z0-9]+$';
^ and $ require the entire string to match rather than just any portion of it, and + looks for 1 or more alphanumberic characters.
You could also use a named character class if you prefer:
SELECT * FROM table WHERE column REGEXP '^[[:alnum:]]+$';
Try this:
REGEXP '^[a-z0-9]+$'
As regexp is not case sensitive except for binary fields.
There is also this:
select m from table where not regexp_like(m, '^[0-9]\d+$')
which selects the rows that contains characters from the column you want (which is m in the example but you can change).
Most of the combinations don't work properly in Oracle platforms but this does. Sharing for future reference.
Try this
select count(*) from table where cast(col as double) is null;
Change the REGEXP to Like
SELECT * FROM table_name WHERE column_name like '%[^a-zA-Z0-9]%'
this one works fine
Using phpmyadmin and mysql I need to select records containing the string '&pageid=page' in order to replace it with a null string.
I tried
SELECT * FROM `wp_posts` where post_content like '%&pageid=page%'
but no record was selected.
I tried again with without the & like explained below
SELECT * FROM `wp_posts` where post_content like '%pageid=page%'
and it worked, so I had some records selected. At this point, I suppose that the '&' char must be handled in a different way. Any idea to solve this?
Use keyword escape to mention escape character for the query.
select stuff from mytab
where
mycol like '%\_to\_%' escape '\';
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'm trying to find all entries that contain a backslash anywhere, like so:
SELECT * FROM animals WHERE bodyType LIKE '%\\%'
I also tried:
SELECT * FROM animals WHERE bodyType LIKE '%\\\\%'
and
SELECT * FROM animals WHERE bodyType LIKE '%\\\\\\\\%'
Neither worked. Anyone know how to do this?
I am running the commands in MySQL Quick Admin v1.5.4
This question has not answer.
You try to match searching of content with backslash on names of columns.
Your (a bit general) query
SELECT * FROM tableName WHERE columnName LIKE '%\\%'
will give you any result if content of any column will contain backslash. In this case your query is correct. But you cannot match name of any column.
I looked into some books I have and all they say the same: this will select all records written in column of chosen name that are containing backslash. But columns have to be chosen exactly (they cannot be selected by name with using of SQL query).