I am new to SQL and still learning but one thing I am confused about is where we use ` and ' operators in MySQL/MariaDB. Can anyone explain this?
Backticks (`) are used to indicate database, table, and column names. Unless you're using reserved or conflicting words for table and database names, you'll not need to use them.
Quotes (' or ") are used to delimit strings, and differentiate them from column names.
For example:
SELECT * FROM `database`.`table` WHERE `column` = "value";
As I mentioned, backticks aren't needed, if you use reasonable table and column names:
SELECT * FROM mydb.users WHERE username = "jim";
But strings will always need quotes. This query is comparing the value in the column username against a value in the column bob, rather than the string value "bob":
SELECT * FROM mydb.users WHERE username = bob;
Related
Today in a live learning session with a friend of mine Dumisani Ndubane, we found out a slight change in behavior when using ORDER BY to sort a result set with column alias by using the ``, '' or "" quote types.
'' and "" aren't affect by the sorting, only `` works but all quotes are allowed to do column aliasing. See queries below;
This sort the Full name in ascending order (using `` quotes on column alias):
SELECT
CONCAT_WS(', ', lastName, firstname) `Full name`
FROM
employees
ORDER BY
`Full name`;
This doesn't sort the result set. Note we used '' quotes
SELECT
CONCAT_WS(', ', lastName, firstname) 'Full name'
FROM
employees
ORDER BY
'Full name';
This doesn't sort the result set. Note we used "" quotes
SELECT
CONCAT_WS(', ', lastName, firstname) "Full name"
FROM
employees
ORDER BY
"Full name";
Also, we where using MySQL version mysql Ver 8.0.19 for osx10.13 on x86_64 (Homebrew) on Mac. Is this intentional, is there an explanation to this behavior? Also, why not stick with backticks(``) with MySQL identifiers and '' or "" for string literals. Why mix them?
I think this could be a user experience (UX) improvement for MySQL because the current status quo seems confusing for a newbie trying to learn.
What do the SO community think and thanks for your help in advance.
Single quotes (and, in MySQL, double quotes) stand for literal strings. So 'Full name' is just that: a literal string. Using that for sorting makes no sense, since the value is constant for all rows: as a result, the ordering of rows is undefined, meaning that the database is free to return rows in whatever order it likes.
Instead, use backticks, that are used for identifiers, so the order by refers to the expression aliases in the select clause.
Or better yet, use an alias that does not requires quoting, so you don’t have to worry about this all.
The second example uses
ORDER BY 'Full Name'
This is a string literal, which is a constant value. It does not refer to the column alias of the same characters.
Any ORDER BY of a constant value results in an arbitrary order, because every row has an equal chance of being ordered before any other row. They are all tied.
Double-quoted strings are also treated as string literals by default in MySQL. This is different from ANSI SQL, where double-quotes are identifier delimiters. MySQL does that if you set sql_mode=ANSI or sql_mode=ANSI_QUOTES.
I have a table, one of the columns contains a text values, some of which are comma separated string, like this:
Downtown, Market District, Warehouse District
I need to modify my query to see is a given value matches this column. I decided that using IN() is the best choice.
SELECT *
FROM t1
WHERE myValue IN (t1.nighborhood)
I am getting spotty results - sometimes I return records and sometimes not. If there's a value in t1.nighborhood that matches myValue, I do get data.
I checked and there are no MySQL errors. What am I missing?
You can use FIND_IN_SET() to search a comma-delimited list:
SELECT *
FROM t1
WHERE FIND_IN_SET(myValue, REPLACE(t1.nighborhood, ', ', ','));
The REPLACE() is necessary to remove the extra spaces.
Another solution is to use regex to match your search value surrounded by commas if necessary:
SELECT *
FROM t1
WHERE t1.nighborhood REGEXP CONCAT('(^|, )', myValue, '(, |$)');
In general, it's bad design to store distinct values in a single column. The data should be normalized into a related table with a foreign key.
SELECT * FROM tbl_custum_advertisement WHERE 'group' = 1
SELECT * FROM `tbl_custum_advertisement` WHERE `group`=1
You first query is comparing a literal 'group' to a literal 1.
Your second query is comparing the value of the field group to a literal 1.
MySQL uses backticks to indicate fields, and single quotes to indicate strings (or double quotes, actually)
Backticks (`) are to be used as an identifier for tables and columns
Single quotes (') should be used for strings and string comparisons
Please refer to this question for further understanding
When to use single quotes, double quotes, and backticks in MySQL
Quotes (') and backticks
`
are different.
'blah' is a literal string. But
`blah`
is a column (or other) name.
'group' = 1 is never true. But a column named group might contain the value 1, so
`group` = 1
could possibly be true.
I am facing a problem while using Select statement where I have a column name as 'Value', and I want to retrieve the values based on the 'value' column with SELECT and LIKE operators.
Code:
SELECT compo.clecompo
FROM compo compo
ON (compo.clecompo = metadatas_compo.clecompo)
AND ((metadatas_compo.value LIKE '%%NOM%%')
OR (metadatas_values.metavalues_name LIKE '%%NOM%%'))
I highlighted the value keyword.. It's a sample of my query where i am not getting any results.
Note: (metadatas_compo.value) metadatas_compo is table name.
You escape literals in MySQL using backticks `
SELECT compo.clecompo FROM compo compo ON (compo.clecompo = metadatas_compo.clecompo) AND ((metadatas_compo.`value` LIKE '%%NOM%%') OR (metadatas_values.metavalues_name LIKE '%%NOM%%'))
However, it is advised to not use reserved literals in your table/column names. For a list of reserved words:
http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html
I want to fetch all records which has one column contained % sign in mysql
we can do this using mysql using like
for ex..
select * from table where column like '%%';
it returns all records..
Please suggest
Use a backslash to escape the percent:
select * from table where column like '%\%%';
will match any row containing a percent character
% is a special character, try using escape characters to find it. Right now you're just telling mysql to look for a string using 2 wildcard characters (%) as opposed to the actual '%' character. Try using
select * from table where column = 'a%' ESCAPE 'a'
Basically telling mySQL to "Look for the string 'a%', but remove the char a in front of it.
EDIT: Another option is just using
select * from table where column = '\%'
Doing the same thing on later mySQL versions. The backslash is the "standard" escape character.
EDIT 2: Or to actually answer your question:
select * from table where column = '%\%%'
You need to escape the literal % sign with a \ e.g.
select * from table where column like '\%%';