Why does DISTINCT has to go first in MySQL?

最后都变了- 提交于 2019-12-11 04:43:06

问题


I have a query that works when I do

SELECT DISTINCT(table.field.id), 1 FROM ... 

but fails when I do

SELECT 1, DISTINCT(table.field.id) FROM ... 

Is this a known behavior?

Why does the first one work while the second doesn't?


回答1:


Unfortunately I'm not able to add a comment yet.

What @Gordon Linoff has written is exactly right. You are getting error as DISTINCT in general works as part of SELECT clause or AGGREGATE function. It is used to return unique rows from a result set and it can be used to force unique column values within an aggregate function.

Examples: SELECT DISTINCT * ... COUNT(DISTINCT COLUMN) or SUM(DISTINCT COLUMN).

More information's about DISTINCT in popular DB engines:

  • PostgreSQL:https://www.postgresql.org/docs/9.0/static/sql-select.html#SQL-DISTINCT
  • SQL Server: https://www.techonthenet.com/sql_server/distinct.php
  • Oracle: https://www.techonthenet.com/oracle/distinct.php
  • MySQL: https://dev.mysql.com/doc/refman/5.7/en/distinct-optimization.html https://dev.mysql.com/doc/refman/5.7/en/select.html


来源:https://stackoverflow.com/questions/43193406/why-does-distinct-has-to-go-first-in-mysql

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!