How to use mysql reserved keywords as column in where statement? - mysql

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'

Related

The mysql problems(select * from my table)

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`;

MySQL wildcard Like query with multiple words

I have a mysql query as follows.
$query="SELECT name,activity FROM appid
where result > 5 AND name LIKE :term ORDER BY name ASC LIMIT 0,40";
$result = $pdo->prepare($query);
$result->bindvalue(':term','%'.$_GET["q"].'%',PDO::PARAM_STR);
$result->execute();
What i want to do is this.
I have and entry like this that i want to find
'News & Weather'
However when i type
'news weather'
it of course will not find it. How can i be able to type that and retrieve that entry?
Regular expressions can do the trick:
select *
from appid
where name rlike 'news|weather' -- Matches 'news' or 'weather'
Another example:
select *
from appid
where name rlike 'news.*weather' -- Matches 'news' and 'wether'
-- with any characters in-between or none at all
-- (ordered)
Just one more:
select *
from appid
where name rlike '^news.{1,}weather$' -- Matches any text that starts with 'news'
-- and has at least one character before
-- ending with 'weather'
Regular espressions can be used to create very complicated filters on text fields. Read the link above for more information.
If you can add a full-text index to your table, Full-text search might be the better way to go with this. Specifically, a boolean Full-Text search:
select *
from appid
where match(name) against (+news +weather)
I believe the only way possible are through code:
Option A: Replace the spaces in your query parameter with '%' in code, but that of course will make the multiple words ordered
Option B: Split your parameter on spaces and dynamically construct your query with as many LIKEs as needed, adding additional ":termN" parameters for each one.

Full JOIN MySQL Query is returning empty

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
)

Simple WHERE clause for column name 'User ID'?

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';

Basic MySQL problem

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'