问题
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