This question already has answers here:
Equals(=) vs. LIKE
(16 answers)
Why is there is a difference between equality comparison between equal operator and like operator?
(3 answers)
Difference between SQL LIKE without percent signs and equal (=) in WHERE clause
(2 answers)
Closed 4 years ago.
I have the below MySQL query.
select * from node where title LIKE 'born in care shelter breed';
Which is returning empty set. But when I try the below query
select * from node where title = 'born in care shelter breed';
It is returning 1 result.
What difference the both will make? I can't avoid the LIKE operator as the query creating after some condition checking
I'm guessing you have trailing whitespace in your title field. MySQL string comparison using the = sign does not consider trailing whitespace. Note the following:
CREATE TABLE node
(title VARCHAR(99));
INSERT INTO node (title)
VALUES ('born in care shelter breed '); -- Note the space at the end
SELECT * FROM node WHERE title LIKE 'born in care shelter breed';
SELECT * FROM node WHERE title = 'born in care shelter breed';
Notice how the first select statement returns zero results, but the second one finds the row.
SQL Fiddle
The documentation talks about this on the string comparison page, stating:
In particular, trailing spaces are significant, which is not true for
CHAR or VARCHAR comparisons performed with the = operator
The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.
There are two wildcards used in conjunction with the LIKE operator:
% - The percent sign represents zero, one, or multiple characters
_ - The underscore represents a single character
i think below will return row
select * from node where title LIKE '%born in care shelter breed%';
To know about like more
Related
This question already has answers here:
Find value in comma delimited string mysql
(3 answers)
Closed 6 years ago.
I've got a field in my database which contains comma-separated integers, for example:
"4,6,18,26,29,34"
I need to construct an SQL query which will return the record which contains a specific given integer, for example 6, so my current query is like this:
SELECT * FROM mytable WHERE CSVField LIKE "%,6,%"
I've surrounded the desired value with commas to avoid 6 matching 26 however it's obvious that my current query won't match against the first or last values in the field because the field doesn't start or end with a comma, so in the example above it'll never find 4 or 34.
How can I write my query so that it'll do what I want?
You can try:
SELECT * FROM mytable WHERE CSVField LIKE "%,6,%"
OR CSVField LIKE "6,%"
OR CSVField LIKE "%,6"
Hope you table is quite small though.
UPDATE
Following Mihai's post, try:
SELECT * FROM mytable WHERE FIND_IN_SET(CSVField,'6') > 0
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
MySQL - If It Starts With A Number Or Special Character
Select values that begin with a number
I'm trying to create an ORDER BY statement that checks to see if the column begins with a number, if it does it should add + 0 to the column. If not, it does not.
Something like this IF(title begins with number, title + 0, title)
Not sure how to go about this.
ORDER BY IF(title REGEXP '^[0-9]',title+0,title)
But, that expression is going to return a numeric value; and since "title+0" is essentially equal to "title" (in terms of a numeric value comparison), I don't think that's going to do what you want it to.
(The "+0" operation will add zero to the leading numeric portion of the string value, and return a numeric value; so the entire expression will be evaluated as a numeric value. Which means that the bare "title" will also be a numeric value, which will be returned as 0 if there is no leading numeric.
So, that is essentially equivalent to:
ORDER BY title+0
In what order do you actually want the title values returned in? Do you want the rows returned in order by the leading numeric, and then by the title string?
I'm thinking you want to order by the string value of title when there isn't a leading numeric digit; otherwise, by the numeric value of leading numeric digits, and then the string value of title, so something like this might get you closer to what you want:
ORDER BY title REGEXP '^[0-9]', title+0, title
You could force the title to be interpreted as an integer and test that it's >= 1:
ORDER BY IF(title+0 >= 1, title+0, title)
But I have to comment that it sounds like you're using one column for two different purposes, which is a no-no in relational database design.
Also it's going to be really slow, since the ORDER BY can't benefit from an index.
if title LIKE 'number%' then
CONCAT(title, '0');
end if;
if (left(title,1) regexp '[0-9]', title+0, title)
or
if (title regexp '^[0-9]', title+0, title)
This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 8 years ago.
select concat(Sno,Table) as STB from levels
Above query gives error if run as it is. Say i have valuse in levels as
Sno Table
1 Sale
2 Stock
I need to fetch them as
STB
---
1Sale
2Stock
What can be the solution other than changing the column name because putting quotes around the word 'Table' gives the wrong output as it becomes just a string
Use backticks for reserved words.
select concat(Sno, `Table`) as STB from levels
Though in general, if you can avoid using reserved words for database, table, or column names in the future, that's a good idea.
select concat(Sno,`Table`) as STB
from levels
Try with ` instead of ' like this :
SELECT CONCAT(Sno,`Table`) AS STB FROM levels
I've been trying to write this query, I need to select the rows where a column has only letters (a-z) and a full stop.
I tried this but it's not working:
SELECT * FROM table WHERE (c1 REGEXP '[^a-zA-Z\.]') = 0
This one would usually work in PHP.
Try:
SELECT * FROM table WHERE c1 REGEXP '^[a-zA-Z.]+$'
The anchor ^ and $ ensure that you are matching the entire string and not part of it. Next the character class [a-zA-Z.] matches a single upper/lower case letter or a period. The + is the quantifier for one or more repetitions of the previous sub-regex, so in this case it allows us to match one or more of either a period or a upper/lower case letter.
More info on regex usage in MySQL
This question already has answers here:
MySQL query finding values in a comma separated string
(11 answers)
Closed 5 years ago.
I work with a system that used comma separated values as a one-2-many relation. It is stored in as a blob.
I try to use the MySQL IN (LIST), but only ends up with the rows where the uid is first in the list or only one in the list.
mytable
uid categories
1 24,25,26
2 25
3 22,23,25
4 25,27
run sql:
SELECT * FROM mytable WHERE 25 IN (categories)
Result:
uid categories
2 25
4 25,27
Missing (should have been selected but are not)
uid categories
1 24,25,26
3 22,23,25
Any idea why a string has to start with 25 and not just contain 25? I guess it is a string issue rather than an IN (LIST) issue
UPDATE - based on answers below:
When using th IN (LIST) on a blob, the blob is converted to an int and commas and "digits" are lost.
In stead use FIND_IN_SET(needle, haystack) that will work also on a blob containing comma separated values.
I think you are looking for FIND_IN_SET
SELECT * FROM mytable WHERE FIND_IN_SET(25,category)
This should give you your asnwer:
SELECT * FROM mytable WHERE CONCAT(',', categories, ',') LIKE '%,25,%'
But it would make more sense to create a connecting table, with each comma separated value as a new row.
What's happening is that MySQL is converting the blob to an integer (not a list of integers) for comparison. So '24,25,26' becomes the value 24. You can confirm this by running the query
SELECT CAST(categories AS signed) FROM mytable
This is not how IN (nor the blob data type) is meant to be used. Is there a reason you're not using another table for this?
Yes, it's astring issue. Your 'list' is just a string to mysql, meaning nothing.
You would have to use RegEx.
But you might think about normalizing your tables instead.