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';
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 want to get a column from my table, but something wrong.
enter image description here
enter image description here
This problem is strange, I select * from order(a table of my database), but I cannot do that, I don't know where is wrong.
order is a reserved keyword in MySql where it is generally used to order output with reference to particular coloumn.
General logic of any coding or database structure is to avoid reserved keywords by that particular coding language or databases.
check the following link for reserved keywords in mysql
https://dev.mysql.com/doc/refman/5.5/en/keywords.html
In your case to resolve the issue , simply use back tick (" ` ") for the word order i.e.
select * from `order`
order is a reserved word in SQL. You will need to escape such a table name in your SQL:
SELECT * FROM `order`;
But it would be better would be to not use that as a table name.
Change the name of the table order or use `` before and after it so the code will be:
SELECT * FROM `order`;
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.
So here is a MySQL Query:
SELECT TestSite . * , LoggedCarts . *
FROM TestSite, LoggedCarts
WHERE TestSite.email = 'LoggedCarts.Bill-Email'
LIMIT 0 , 30
It is returning an empty result set, when it should be returning four results based on the tables below.
First Table: LoggedCarts - Column: Bill-Email
casedilla#hotmail.com
crazyandy#theholeintheground.com
Second Table: TestSite - Column: email
samuel#lipsum.com
taco#flavoredkisses.com
honeybadger#dontcare.com
casedilla#hotmail.com
messingwith#sasquatch.com
The goal is to get a MySQL statement that returns the rows in Table: TestSite that don't match the rows in Table: LoggedCarts.
Note: I understand that the use of a hyphen in a column name requires special care when constructing a query, involving backticks to tell MySQL there are special characters. I would change the column names to match up, however the Table: LoggedCarts has data fed via post from a Yahoo Shopping Cart and without heavy preparation before insertion setting the name to anything but the key sent in the post data is daunting.
However, if it turns out rebuilding the data prior to insertion is easier than using a JOIN statement or for some reason using two columns with different names as the comparison columns just doesn't work, I will go through and rebuild the database and PHP code.
Single quotes indicate a string literal. You need to use backticks for identifiers. Also, each component of an identifier must be quoted individually.
SELECT TestSite . * , LoggedCarts . *
FROM TestSite, LoggedCarts
WHERE TestSite.email = LoggedCarts.`Bill-Email`
LIMIT 0 , 30
From the manual:
If any components of a multiple-part name require quoting, quote them individually rather than quoting the name as a whole. For example, write `my-table`.`my-column`, not `my-table.my-column`.
With a bit of research inspired by somne of the hints given, I found the solution I was looking for here: SELECT * WHERE NOT EXISTS
Does exactly what I need it to do, and as a bonus, I like the shorthand syntax that is used that allows you to put in an alias for the table name and use the alias throughout the statement.
SELECT *
FROM TestSite e
WHERE NOT EXISTS
(
SELECT null
FROM LoggedCarts d
WHERE d.`Bill-Email` = e.email
)
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.