concat columns including reserve word [duplicate] - 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

Related

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

MySQL "LIKE" without "%" is not working same as "=" condition [duplicate]

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

MySQL - How to search for exact word match in a column of sentence? [duplicate]

This question already has answers here:
Search string by exact word in Mysql
(9 answers)
Closed 5 years ago.
I have a MySQL table with a list of sentences such as:
id | Sentence
---+---------------------------------------------------------------
1 | Come On Boys, You Can do it my boy.
2 | Everything is possible, impossible itself says I am possible.
3 | Boys I know possible solutions are good!
4 | Possible solutions are all failed its also possible
I have to search on Sentence column exact word like possible.
There are some condition :
Searching word can't be prefix or suffix of any word.
Searching word possible , impossible is incorrect.
Punctuation mark can be before or after the word.Ex: !boy or boy,
No case sensitive means for possible Possible is correct.
I try a query but it is not working
mysql_query("SELECT * FROM products WHERE Sentence LIKE '%".$search."%'");
Possible punctuation mark are ,.!?;-
I need a working solution. Thanks in advance !
You could use REGEXP:
SELECT *
FROM products
WHERE Sentence REGEXP '[[:<:]]possible[[:>:]]'
-- ^^^ ^^^ word boundaries
This would correspond to matching possible surrounded by a word boundary. Note in the demo that punctuation counts as a word boundary.
Also note that MySQL's REGEXP is case insensitive, so the above should match possible or Possible.
Demo here:
Rextester
Use REGEXP:
SELECT *
FROM products
WHERE Sentence REGEXP '[^\\d\\w]possible[^\\d\\w]'
This takes every word possible surrounded by any word or number.
Be carefull with case sensitivity.
CREATE TEMPORARY TABLE tmp (id INTEGER PRIMARY KEY AUTO_INCREMENT, sentence VARCHAR(200));
INSERT INTO tmp (sentence) VALUES ('Come On Boys, You Can do it my boy.');
INSERT INTO tmp (sentence) VALUES ('Everything is possible, impossible itself says I am possible.');
INSERT INTO tmp (sentence) VALUES ('Boys I know possible solutions are good!');
INSERT INTO tmp (sentence) VALUES ('Possible solutions are all failed its also possible');
SELECT * FROM tmp WHERE sentence REGEXP '[[:<:]]impossible[[:>:]]';
This is duplicate: Search for "whole word match" in MySQL

How do I find the records containing a comma-separated value? [duplicate]

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

mysql in list only validates first id in list. maybe a blob issue [duplicate]

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.