(SELECT * FROM (SELECT clientid,totalquantity from clientquantity
order by quantitydate desc) AS A GROUP BY clientid);
和
SELECT clientid,totalquantity from clientquantity group by clientid;
有人可以解释一下上面两个查询之间的区别吗?结果集在计数的行方面是相同的。那么,这两个查询之间有区别吗?例如,性能方面,还是数据方面的差异?
第一个查询用作与另一个表的 LEFT JOIN
查询的一部分。为了简单起见,我在这篇文章中删除了另一个表格。
Is the above because the 2nd query needs to contain an ORDER BY as well and with SQL the only way to do that is to have do it like done in 1st query? I have omitted ORDER BY from the 2nd query but it is required in the 2nd query as well.
谢谢,
最佳答案
检查这个SQL FIDDLE DEMO了解您的两个查询之间的区别。
当您使用按日期降序排列的数据排序时 使用 clientId 对该数据进行分组,而不是检索所有客户端数据 最新日期。
如果您直接使用
GROUP BY
子句那么它将获取每个第一个 按 clientId 从表中记录。
关于MySQL - 两个查询之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14064682/