This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 3 years ago.
Im trying to write a query in MySQL however one fo my column names is 'comment' however when entered into a WHERE clause it shows up bold and doesn't get used as a column name does anyone know how to change that?
this is the query
SELECT DISTINCT propertyNo from Viewing
WHERE comment IS NULL
UNION
SELECT propertyNo FROM PropertyForRent
WHERE rent < 600
ORDER BY propertyNO ASC;
You need to quote it:
WHERE `comment` IS NULL
This is covered in the Schema Object Names sections of the MySQL 5.7 Reference Manual.
Always use backticks and quotation marks when you write your SQL.
With ` you write variable names
With ' you write variable values
For example
SELECT * FROM `test` WHERE `x` = 'blahblah'
COMMENT is a keyword in MySQL. See: https://dev.mysql.com/doc/refman/8.0/en/keywords.html#keywords-8-0-detailed-C
That's why your editor shows it as bold. You can escape this by using back ticks:
SELECT DISTINCT `propertyNo` from `Viewing`
WHERE `comment` IS NULL
Try to always use back ticks when referring to columns or table names. More info on that subject: https://dba.stackexchange.com/questions/23129/benefits-of-using-backtick-in-mysql-queries/23130
Related
This question already has an answer here:
Search with comma-separated value mysql
(1 answer)
Closed 4 years ago.
I could use help with the database query I am trying to pull off. I have a table that has a word or phrase, a product id, a language id, and a version id. The version_id is a comma separated column, since the same word can be used in multiple versions and I tried to avoid adding another row with same data except for a different version.
The table currently looks like this
I am trying to get the COUNT of word per product, version, and language. The problem I am getting is seperating the version_id since it is a comma seperated column. Is there a way to get the count for all words in version 1, and version 2, separately?
I currently have this query, which works, but it does not separate by versions in a comma separated column.
SELECT COUNT( translation ) AS translations, fdp.name AS product, fdv.name AS version, fdl.name AS language, fdl.position
FROM `fdnfortidictionary_dictionary` AS `fdd`
LEFT JOIN `fdnfortidictionary_product` AS `fdp` ON fdd.product_id = fdp.id
LEFT JOIN `fdnfortidictionary_version` AS `fdv` ON fdd.version_id = fdv.id
LEFT JOIN `fdnfortidictionary_language` AS `fdl` ON fdd.language_id = fdl.id WHERE fdp.enable = 1
AND fdd.product_id IN (1,2)
AND CONCAT( ",", fdd.version_id, "," ) REGEXP ",(1|2|3|4|5),"
GROUP BY `language`,`version`,`product`,`position`
ORDER BY fdl.position ASC
The Result I am looking for is something like,
It seems like you might have some luck with FIND_IN_SET()
Probably, something like FIND_IN_SET(fdd.version_id, fdl.version_id) > 0
I am trying to select a record which clearly exists, but my SQL query does not bring it up. Any idea how to get this working?
SELECT * FROM Users WHERE 'local.email'='burgundy#email.com' LIMIT 1
The issue is that you're using single quotes ( ' ) around your column name, rather than using backticks ( ` ).
Try using this instead:
SELECT *
FROM Users
WHERE `local.email` = 'burgundy#email.com'
LIMIT 1
Like Crocodile said, anything that is a SQL variable like a table name or column name can also be surrounded by `` Back ticks (hold shift and hit ~). This tells SQL to look at them as literals.
This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 8 years ago.
I have a problem when I execute this query:
SELECT 'preg', 'respA', 'respB', 'respC', 'respD', 'respV', 'subTema'
FROM comun
WHERE 'id' = 3
In the table id is of type INT.
PHPMyAdmin says that the query is correct but returns 0 rows and the table has 4 rows.
What is the error?
Thank you very much!
Use backticks to escape column and table names, not quotes.
SELECT `preg`, ...
FROM `comun`
WHERE `id` = 3
Quotes indicate a static string. And it returns zero rows because the static string id is not equal to 3.
But actually you only need to escape reserved words in MySQL.
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
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.