Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unknown column in order clause

#1054 - Unknown column 'default_ps_products.manufacturer_id' in 'order clause'

Why am I getting the above error with the statement below it works fine without the p in the statement and I am not using an order clause?

SELECT * FROM `default_ps_products` p WHERE p.`manufacturer_id` = 2
like image 932
Jess McKenzie Avatar asked Jun 26 '13 05:06

Jess McKenzie


People also ask

What is the meaning of unknown column in field list?

By Tech-Assured. January 27, 2021 22496 Views. If you see this error, it means you have forgotten something while creating table.

How do I fix error 1054 in MySQL?

To fix the 1054 error caused by an UPDATE statement, you need to look into the SET and WHERE clauses of your statement and make sure that the column names are all correct. You can look at the error message that MySQL gave you to identify where the error is happening.

Does order matter in order by clause?

The order of columns/expressions showing up does matter. It orders by the first one as specified, then orders that set by the next one, then by the next. The result is different than if you were to reverse the order in which the columns are ordered.


2 Answers

To solve this use SELECT p.* FROM instead of SELECT * FROM.

The reason is that phpMyAdmin is adding an ORDER BY to your query for the first column in the results grid. Because of the alias, the code that does this fails.

This issue reproduces on phpMyAdmin 4.0.6. I don't know the status on the latest 4.2.5

like image 114
Yehuda Adler Avatar answered Sep 22 '22 08:09

Yehuda Adler


Since you posted a partial query this wasn't obvious from the start, but your full query makes it clear;

SELECT *
FROM default_ps_products
WHERE manufacturer_id=2
ORDER BY `default_ps_products`.`manufacturer_id` ASC
LIMIT 0, 30

When you add an alias to default_ps_products table in the select, you can't selectively use the alias only in the WHERE clause, you'll also need to change the ORDER BY to use the same alias. The full query should in other words be;

SELECT *
FROM default_ps_products p
WHERE p.manufacturer_id=2
ORDER BY p.`manufacturer_id` ASC
LIMIT 0, 30
like image 23
Joachim Isaksson Avatar answered Sep 19 '22 08:09

Joachim Isaksson