MySQL: Unknown column in where clause error

蹲街弑〆低调 提交于 2019-11-30 20:27:46

In MySql, backticks indicate that an indentifier is a column name. (Other RDBMS use brackets or double quotes for this).

So your query was, "give me all rows where the value in the column named 'uname' is equal to the value in the column named 'test'". But since there is no column named test in your table, you get the error you saw.

Replace the backticks with single quotes.

Weird? How so? It says exactly what's wrong. There is no 'test' column in your table. Are you sure you have the right table? 'tutorial.users' ? Are you sure the table isn't named differently? Maybe you meant to do

SELECT * from users WHERE uname = 'test';

You have to reference only the table name, not the database.. assuming the database is named tutorial

Thierry Vincent

example:

$uname = $_POST['username'];
$sql="SELECT * FROM Administrators WHERE Username LIKE '$uname'"

Note the single quotes around the $uname. When you echo the query, this is the output-

SELECT * FROM Administrators WHERE Username LIKE 'thierry'

However if you miss the quote around the $uname variable in your query, this is what you'll get-

SELECT * FROM Administrators WHERE Username LIKE thierry

On MySQL server, the 2 queries are different. thierry is the input string and correctly encapsulated in quote marks, where as in the second query, it isn't, which causes an error in MySQL.

I hope this helps and excuse my englis which is not very good

I had the same issue and it turned out to be a typo. My error message was:

Unknown column 'departure' in 'where clause'

I checked that very column in my table and it turns out that, I had spelt it as "depature" and NOT "departure" in the table, therefore throwing the error message.

I subsequently changed my query to:

Unknown column 'depature' in 'where clause' 

and it worked!

So my advise clearly is, double check that you spelt the column name properly.

I hope this helped.

I also faced the issue of "Unknown column in where clause" when executing the following from linux (bash) command line.

mysql -u support -pabc123 -e 'select * from test.sku where dispsku='test01' ; '

This is what I got

ERROR 1054 (42S22) at line 1: Unknown column 'test01' in 'where clause'

I had to replace the single quotes 'test01' with double quotes "test01" . It worked for me. There's a difference how and the way you're executing sql queries.

When assigning a value to a variable in a script and later, using that variable in a sql statement that has to be executed by the script, there's slight difference.

If suppose variable is

var=testing

and you want to pass this value from within script to mysql, then single quotes work.

select '$var'

So different engines might evaluate backticks and quotes differently.

This is my query that worked from linux command line.

mysql -u support -pabc123 -e 'select * from test.sku where dispsku="test01" ; '
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!