需要在mysql查询功能中通过一些限制和命令代码的解决方案

我有这样的桌子

|post_date |post      |value    |
---------------------------------
|2019-11-05|post      |text     |
|2019-11-05|post      |text     |
|2019-11-05|post      |text     |
|2019-11-05|post      |text     |
|2019-11-06|post      |text     |
|2019-11-06|post      |text     |
|2019-11-07|post      |text     |
|2019-11-08|post      |text     |
|2019-11-08|post      |text     |
|2019-11-08|post      |text     |
|2019-11-08|post      |text     |
|2019-11-08|post      |text     |
|2019-11-08|post      |text     |

我试图选择这样

SELECT post_date,count(*) as post FROM myTable GROUP BY post_date ORDER BY post_date ASC LIMIT 3

它会这样显示

|post_date |post      |
-----------------------
|2019-11-05|4         |
|2019-11-06|2         |
|2019-11-07|1         |

我需要像这样的东西

|post_date |post      |
-----------------------
|2019-11-06|2         |
|2019-11-07|1         |
|2019-11-08|6         |

它看起来像是极限3的结果。它将从订单中获取最新的3条结果,而不是最早的结果

jiajia2xm 回答:需要在mysql查询功能中通过一些限制和命令代码的解决方案

只需将当前查询包装在子查询中即可强加所需的升序:

SELECT
    post_date,post
FROM
(
    SELECT post_date,COUNT(*) AS post
    FROM myTable
    GROUP BY post_date
    ORDER BY post_date DESC
    LIMIT 3
) t
ORDER BY post_date;

screen capture of demo below

Demo

本文链接:https://www.f2er.com/3146684.html

大家都在问