Why doesn't SELECT * FROM users_meta where user = 1 and key = tagline work?
It works without the key = tagline. But I need that!
users_meta is a table with these fields:
id, key, value, user.
I'm trying get the value from a row with key = tagline and user = 1.
Thanks!
It is because key is a MySQL keyword. If it is also a column name in your table, then you need to surround it in backticks. Like:
`key` = 'tagline'
Syntax. Backticks around reserved words used as identifiers, single quotes around string literals.
SELECT * FROM users_meta WHERE user = 1 AND `key` = 'tagline'
Related
MYSQL won't let queries select rows when the column name is a mysql keyword in the where statement.
I'm creating a php api where it grabs a username that is attached to a key provided by the user but when it's grabbing the username it can't search for it using key == '$key' because "key" is a reserved MySQL word that the frontend generates (and it can't be changed.)
I've tried surrounding the column name in "s, 's, `s, and []s, none of which have worked. If anyone could help me that would be great.
$key = $_GET['key'];
$sql = "SELECT * FROM discordlink WHERE key = '$key'";
$result = $conn->query($sql);
It should be able to find the row attached to the key using the where clause provided but it doesn't becuase it's a mysql reserved keyword.
You can surround the column name with backticks(`) for the reserved mysql words
SELECT `key` FROM `table`
Bit late answer, but still there was no accepted answer I am adding this answer.
We can give alias name for table and refer reserved keyword column in WHERE statement i.e
SELECT * FROM discordlink AS d WHERE d.key = '$key'
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;
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.
I am not able to run a simple select query with a where clause.
I am using MySQL and have a column User ID.
The problem is with the column name made of two words.
select * from user where 'User ID' = "xyz"
A usual query like the next one runs fine as expected:
select * from user where email = 'xyz'
How can I write a condition on the User ID column?
Try:
SELECT u.*
FROM `user` u
WHERE u.`User ID` = 'xyz';
But in general, try not to use such column names.
Using backticks to qualify the table and/or column names is also useful if you have names that conflict with MySQL keywords, e.g. user.
No way to rename this column ?
You can try with backticks around the column name in your query :
select * from user where `User ID` = 'xyz';
Like Jonathan Leffler pointed, if you are in MS SQL Server you can try:
select * from user where [User ID] = "xyz"
or in MySql PHP Admin:
select * from user where ´User ID´ = "xyz"
I am not sure about MySql, but in SQL Server, even "login" or "user" that are some reserved words, they are functional in queries normally. Despite, I think is better not to use that.
I hope this can help you or another one. Hugs.
Ref.:
1. Meaning of square brackets [] in MS-SQL table designer?
2. When to use single quotes, double quotes, and backticks in MySQL
I think user is a reserved word...
Try:
SELECT *
FROM `user`
WHERE `email` = 'xyz';
I have in my table this value ART(\'O\') in the field Subject.
How do I check if this subject exist?
I tried:
select * from table1 where Subject = 'ART(\'O\')';
select * from table1 where Subject = "ART(\'O\')";
Both failed at picking up the record.
How sholud I prhase the query so that the record containing ART(\'O\') will be picked?
Note: Please do not refer the query: select * from table1 where Subject like '%ART(%';
bec they may be other records such as ART(EX), ART(NA),etc... existing
Need to know how to use the Subject = '' method.
Thanks.
If the value contains the backslashes, you probably need to escape them. Otherwise you're looking for value ART('O').
SELECT * FROM table1 WHERE Subject = "ART(\\'O\\')";
Also make sure you don't have trailing whitespace.