What is the error in this SQL query? [duplicate] - mysql

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.

Related

Why Left() returns to 0, but substring () not [duplicate]

This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 2 years ago.
A Practice Q from HackerRank. Very confused about the difference between left() and substring () in the case:
Reference: https://www.hackerrank.com/challenges/the-pads/problem
Considering the following, it is returned to Name(0)
SELECT CONCAT (NAME,'(',LEFT('Occupation',1),')')
FROM OCCUPATIONS
ORDER BY NAME
but the following is correct
SELECT CONCAT (NAME,'(',substring('Occupation',1,1),')')
FROM OCCUPATIONS
ORDER BY NAME
Neither is correct. 'Occupation' (with single quotes) is the string that starts with the letter O, which is what both of your examples should yield.
You want "OCCUPATION" (with double quotes) or more commonly OCCUPATION (with no quotes) instead, which is a column name.
LEFT(..., 1) and SUBSTRING(..., 1, 1) are equivalent; your error is elsewhere.

MySQL column names [duplicate]

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

Find if value greater than a number exists in comma separated string in MySql Column [duplicate]

This question already has answers here:
Is storing a delimited list in a database column really that bad?
(10 answers)
Closed 4 years ago.
i have a table with values given below
payment_pattern application
0,0,117,9,5 XXXX0004DqjBQAS
0,30,0,29,16,0 XXX000004E79tQAC
30,30,23,29,22,1 XXX000006F2brQAC
0,0,0,0,29,28 XXXB000006Fs3oQAC
Need to find regular expression that would return all rows with values greater than or equal to 30.
Example output should be
0,0,117,9,5 XXXX0004DqjBQAS
0,30,0,29,16,0 XXXX0004E79tQAC
30,30,23,29,22,1 XXXX0006F2brQAC
You could use REGEXP here:
SELECT payment_pattern, application
FROM yourTable
WHERE payment_pattern REGEXP '[[:<:]]([3-9][0-9]|[1-9][0-9][0-9][0-9]*)[[:>:]]';
But, you would do better to not store unnormalized CSV data in your tables. Your query would be trivial if each number were stored in a separate record.
Here is a demo for the above regex:
Demo
Here is a brief explanation of the above regex:
(
[3-9][0-9] match 30 to 99
[1-9][0-9][0-9][0-9]* match 100 to 999, or this plus any other digit
)

how to select rows with comma and single quotes string in mysql? [duplicate]

This question already has answers here:
How to quote values using group_concat
(3 answers)
Closed 5 years ago.
SELECT GROUP_CONCAT(DISTINCT student.className)
FROM student
WHERE student.studentId in(1,2,3,4,5,6,7,8)
this example select class names as 1B,1F,2F,3F
but, i want to select row with single quotes and comma like,
'1B','1F','2F','3F'
how to solve this?
save this query in variable and fetch using while loop
and save data in variable and print using echo json_encode(variable)

concat columns including reserve word [duplicate]

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