我正在尝试获取最后三组在同一日期创建的优惠券。我的桌子看起来是这样的:

+-------------+-----------+
|coupon name  |Created at |
+-------------+-----------+
|c1           |2016-01-17 |
|c2           |2016-01-17 |
|c3           |2016-01-18 |
|c4           |2016-01-19 |
|c5           |2016-01-19 |
|c6           |2016-01-20 |
|c7           |2016-01-20 |
|c8           |2016-01-20 |
|c9           |2016-01-20 |
|c10          |2016-01-21 |
|c11          |2016-01-21 |
+-------------+-----------+

我的结果应该是:
+-------------+-----------+
|coupon name  |Created at |
+-------------+-----------+
|c4           |2016-01-19 |
|c5           |2016-01-19 |
|c6           |2016-01-20 |
|c7           |2016-01-20 |
|c8           |2016-01-20 |
|c9           |2016-01-20 |
|c10          |2016-01-21 |
|c11          |2016-01-21 |
+-------------+-----------+

我也找过同样的问题,但找不到。
提前谢谢你,

最佳答案

试试这个:

SELECT t1.`coupon name`, t1.`Created at`
FROM mytable AS t1
INNER JOIN (
  SELECT `Created at`
  FROM mytable
  GROUP BY `Created at`
  ORDER BY `Created at` DESC LIMIT 3
) AS t2 ON t1.`Created at` = t2.`Created at`

上面的查询使用包含表的最后三天的派生表。我们可以通过加入这个在线表格来获得最后三组优惠券。
Demo here

关于mysql - 如何获得同时创建的最后三个组?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35259402/

10-10 19:06