Unknown column in order clause - mysql

#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

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

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

Open your phpmyadmin. Click on your selected database. Now you have a list of all tables on right side. Click on structure of default_ps_products table. Now you see a structure of it. Now Click on SQL tab and execute query as 'SELECT * FROM default_ps_products ORDER BY '.
Once you execute this query, Now resolve your problem.

Your query is fine. there is not any error when i run this query. there is nothing wrong with query.
SELECT * FROM default_ps_products AS p WHERE p.manufacturer_id = 2
it working fine.:)

when use #Query(nativeQuery = true), should use underline format ,just like this "Sort.by(Sort.Direction.DESC, "update_time")))", else should use camel properties in entity ,like this "Sort.by(Sort.Direction.DESC, "updateTime")))"

phpmyadmin started showing this error suddenly on one table. only fix was to rename column id to id2 then back. even deleting the table and making new copy didn't help

Related

SQL select: how to alias only 1 column and leave the rest unaliased [duplicate]

I have a table with many columns let say column1,....,column20. I don't want to scroll everytime to the end of the result table to see the value of column20. In mssql I usually do
SELECT column20, * FROM TABLE but apparently this is not valid in MySQL. Any hints? (I also don't want to select all columns explicitly in the select statement)
You have to give the table name in your query, otherwise mysql complains :
SELECT column20, mytable.* FROM mytable
PS: I have absolutely no idea as to why, because SELECT *, column20 FROM mytable works just fine... Strange things happens sometimes ^^

Grouping two mySQL select statements under one alias and ordering that alias

I want to run two mysql SELECT statements, combine them, call the new combination by its own name, then order that new combination by a user-defined function. This is what I am trying currently:
SELECT * FROM (
SELECT * FROM dictionary WHERE def1 LIKE '$input%'
UNION ALL
SELECT * FROM dictionary WHERE def2 LIKE '$input%'
) AS newcol
ORDER BY levenshtein('$input', newcol)
LIMIT 10
But I get the following error:
Unable to run query:Unknown column 'newcol' in 'order clause'
The problem is clearly with defining the new group 'newcol'.
You're trying to order by a TABLE, not by a FIELD! Use a field from your tables ant it'll go smooth

Can not Access Aliased Columns in Select Statement (MySQL)

I have a problem with Aliased Columns in MySQL!
My Query:
SELECT Price AS Pr, (Pr*10/100) FROM MyTable;
MySQL WorkBench Error: UnKnown Column 'Pr' in Field List !!!
I tested my query in W3Schools with no error !
I tested my query in W3Schools with no error!
This doesn't prove that your query is valid.
You can only use aliases in GROUP BY, ORDER BY or HAVING clauses. Your usage variant is not allowed, because the value of alias is not known when MySQL is selecting the 2-nd column.
I've got a suspicion that W3Schools uses MS Access to run user queries, and MS Access does allow such atrocity as referencing column aliases in a SELECT clause that are defined in the same SELECT clause.
The standard doesn't allow this and MySQL does follow standard in this particular case.
As for solution to your problem, I can see two options.
The more generic solution, which would run in probably any SQL product, would be to use a derived table:
SELECT
Pr,
(Pr * 10 / 100) AS SomethingElse
FROM
(
SELECT
SomeComplexExpression AS Pr
FROM MyTable
) AS sub
;
The other option would be to use a variable, which is MySQL-specific:
SELECT
#Pr := SomeComplexExpression AS Pr,
(#Pr * 10 / 100) AS SomethingElse
FROM MyTable
;
Finally, if you need to test/demonstrate if something can/cannot work in MySQL, I'd recommend using SQL Fiddle.

having condition on result of a subquery in mysql

i am trying to run a query like this
SELECT a, b , c, (SELECT INNULL(x,y)) as mycol WHERE mycol < 400 ;
BUt it gives the error
#1054 - Unknown column 'mycol' in 'where clause'
What would be the right way to do this?
Thanks.
It's so in MS SQL, so I assume, that the same problem is in MySQL.
Try to change WHERE to HAVING. WHERE clause doesn't see your renamed columns.
HAVING is working the same way as WHERE, like (mycol < 400 AND a > 5).
GROUP BY should be before HAVING.
Check the examples in the link.
http://www.databasejournal.com/features/mysql/article.php/3469351/The-HAVING-and-GROUP-BY-SQL-clauses.htm
#hgulyan I doubt your answer. It is not the renaming that prevents one from using WHERE clause, rather it is the subquery. So lets say I have a query:
SELECT id as ID FROM user WHERE ID > 10;
This is going to work perfectly fine.
Now lets say I have one more query:
SELECT name, (SELECT id FROM user_detail WHERE user_id = 20) as ID FROM user WHERE ID > 19;
This particular query will produce error as:
Unknown column ID
So, it's about using subquery and column aliases and not just column aliases.
Thus in this case you will have to use HAVING instead of WHERE.

Embarassingly easy SQL question(s)

I am trying to write what should ostensibly, be relative easy SQL queries - yet, I cant seeem to get them to work.
Is it possible to write a query that looks something like this:
select t.name
from (select *
from mystoredproc(1,2,3) as t)
where t.name = 'foobar'
Two problems with the above query:
1) First off all, the statement above dosent work. My db engine (mySQL) complains:
ERROR 1054 (42S22): Unknown column
't.name' in 'field list'
2) I want to use the returned table t
in a SELF JOIN. However, I dont want
to have to call mystoredproc(...)
again, because it is a VERY
expensive call.
Any one knows how to fix these problems?
BTW, even though I am using mySQL (for now), I would prefer if any proffered SQL snippet was db agnostic (i.e. ANSI SQL)
Replace the ) as t) with ) t, as in
select t.name from (select * from mystoredproc(1,2,3)) t where t.name = 'foobar'
For your second problem: Feed the result from mystoredproc into a temporary table and SELECT from it, then you can make a self join without hassles.