mysql数据去重并排序使⽤distinct和orderby的问题⽐如直接使⽤:
SELECT
  distinct mobile
FROM
  table_a
WHERE
  code = 123
ORDER BY
  a_ime desc
在本地mysql数据库没有错,在线上的数据库就会报如下错(估计是版本的问题):
Expression #1 of ORDER BY clause is not in SELECT list, references column 'table_a.a_time' which is not in SELECT list
distinct和distinctive⼤意是:
order by 的列必须在 select中有。如果直接在select 中添加a_time 列,是不符合题意的
应该增加⼀层嵌套,改成如下:
SELECT
  distinct mobile
FROM (
  SELECT
    mobile
  FROM
    table_a
  WHERE
    code = 123
  ORDER BY
    a_time desc
) as tmp
最后⼀句as map 必须要,如果没有这⼀句,就报语法错误:
Every derived table must have its own alias
每⼀个派⽣出来的表都必须有⼀个⾃⼰的别名
原创⽂章,欢迎转载,转载请注明出处!