Home » Open Source » MySQL » ORDER BY clause is not in SELECT list (MySQL 8.0)
ORDER BY clause is not in SELECT list [message #690225] Fri, 13 December 2024 10:30 Go to next message
Frank Naude
Messages: 4587
Registered: April 1998
Senior Member
Maybe someone here can help me to rewrite the query below. It was working perfectly in MySQL 5, but after upgrading to MySQL 8, is giving an error:

mysql> SELECT DISTINCT thread_id, subject FROM msg WHERE apr=1 ORDER BY post_stamp DESC LIMIT 10;  

ERROR 3065 (HY000): Expression #1 of ORDER BY clause is not in the SELECT list, referencing column 'msg.post_stamp' which is not in the SELECT list; this is incompatible with DISTINCT;
I can easily add 'post_stamp' to the SELECT list, but that will produce a completely different result!
Re: ORDER BY clause is not in SELECT list [message #690227 is a reply to message #690225] Fri, 13 December 2024 12:53 Go to previous messageGo to next message
Michel Cadot
Messages: 68728
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

I'm afraid he is right.

You have to split it in 2:
SELECT DISTINCT thread_id, subject
FROM ( SELECT thread_id, subject 
       FROM msg 
       WHERE apr=1 
       ORDER BY post_stamp DESC 
       LIMIT <something larger enough to then get 10 distinct> )
LIMIT 10; 
Re: ORDER BY clause is not in SELECT list [message #690229 is a reply to message #690227] Fri, 13 December 2024 13:27 Go to previous message
Frank Naude
Messages: 4587
Registered: April 1998
Senior Member
Problem solved. Thank you Michel!

mysql> SELECT DISTINCT thread_id, subject FROM (SELECT thread_id, subject FROM msg WHERE apr=1 ORDER BY post_stamp DESC LIMIT 30) LIMIT 10;
ERROR 1248 (42000): Every derived table must have its own alias
MySQL is full on nonsense - but, this works:

mysql> SELECT DISTINCT thread_id, subject FROM (SELECT thread_id, subject FROM msg WHERE apr=1 ORDER BY post_stamp DESC LIMIT 50) x LIMIT 10;
+-----------+--------------------------------------------------------------------+
| thread_id | subject                                                            |
+-----------+--------------------------------------------------------------------+
|    208718 | Re: New server migration                                           |
|    208719 | Re: ORDER BY clause is not in SELECT list                          |
|    208717 | Re: Help Needed in Calculation Logic function                      |
|    208719 | ORDER BY clause is not in SELECT list                              |
|    208718 | New server migration                                               |
|    208714 | Re: dense_rank() in formula column                                 |
|    208717 | Help Needed in Calculation Logic function                          |
|    208716 | Re: Find out the column values based on primary key from one table |
|    208716 | Find out the column values based on primary key from one table     |
|    208265 | Re: FCBGuard or USB security token login AS SYSDBA                 |
+-----------+--------------------------------------------------------------------+
Previous Topic: Insert data from Oracle 11g to MYSQL
Goto Forum:
  


Current Time: Tue Dec 24 13:10:57 CST 2024