我如何一起使用group by和order by condition

时间:2017-09-14 23:45:57

标签: mysql sql

SELECT n.*, u.* 
  FROM notice AS n 
  LEFT JOIN user AS u 
    ON n.userid = u.user_id 
 WHERE  n.interitor = '13' 
   AND n.userid != '13' 
   AND n.deleted = '0'  
  GROUP BY object_id
 ORDER BY createdate  DESC

我如何一起使用分组和按条件排序

1 个答案:

答案 0 :(得分:0)

您没有要分组的聚合。但如果你这样做会看起来像这样。

SELECT
    SUM(n.column) AS sumcolumn,
    u.column1,
    u.column2
FROM
    notice AS n 
LEFT JOIN
    user AS u 
        ON n.userid = u.user_id 
WHERE
    n.interitor = '13' 
    and n.userid != '13' 
    and n.deleted = '0'  
GROUP BY
    u.column1,
    u.column2
ORDER BY
    u.column1,
    u.column2;