使用 MySQL 选择最后 5 个条目的总和的最大值?

发布于 2024-11-05 08:52:55 字数 601 浏览 0 评论 0原文

嘿,我不是 MySQL 的最大专家,但这是我迄今为止获得 MAX 输入的内容

   SELECT DISTINCT 
          websites.id, 
          websites.title, 
          websites.url, 
          websites.screenshot, 
          impressions.number, 
          blocks.price 
     FROM websites 
LEFT JOIN blocks ON websites.id = blocks.website 
LEFT JOIN impressions ON blocks.id = impressions.block 
    WHERE status = 1 
      AND blocks.active = '1' 
      AND impressions.number = (SELECT MAX(number) 
                                  FROM impressions)

我想要做的是 SELECT max(number) 但最后 5 个条目的总和。我尝试过乱搞,但就是无法得到它。

Hey, I'm not the biggest expert with MySQL but here is what I have so far to get the MAX entery

   SELECT DISTINCT 
          websites.id, 
          websites.title, 
          websites.url, 
          websites.screenshot, 
          impressions.number, 
          blocks.price 
     FROM websites 
LEFT JOIN blocks ON websites.id = blocks.website 
LEFT JOIN impressions ON blocks.id = impressions.block 
    WHERE status = 1 
      AND blocks.active = '1' 
      AND impressions.number = (SELECT MAX(number) 
                                  FROM impressions)

What I want to do is SELECT the max(number) but of the sum of the last 5 entries. I've tried messing around but just can't get it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

明月夜 2024-11-12 08:52:55

最后五个印象。数字这应该做它不知道你想要总结什么,但

SELECT DISTINCT websites.id, 
                websites.title, 
                websites.url, 
                websites.screenshot, 
                impressions.number, 
                blocks.price 
FROM   websites 
       LEFT JOIN blocks 
         ON websites.id = blocks.website 
       LEFT JOIN impressions 
         ON blocks.id = impressions.block 
WHERE  status = 1 
       AND blocks.active = '1' 
ORDER  BY impressions.number 
LIMIT  5 

如果你想总结块。价格你可以这样做

 SELECT SUM(lastblocks.price) 
   FROM (
    SELECT 
          price
    FROM   websites 
           LEFT JOIN blocks 
             ON websites.id = blocks.website 
           LEFT JOIN impressions 
             ON blocks.id = impressions.block 
    WHERE  status = 1 
           AND blocks.active = '1' 
    ORDER  BY impressions.number 
    LIMIT  5 ) lastblocks

The last five impressions.number this should do it don't know what you want to sum on though

SELECT DISTINCT websites.id, 
                websites.title, 
                websites.url, 
                websites.screenshot, 
                impressions.number, 
                blocks.price 
FROM   websites 
       LEFT JOIN blocks 
         ON websites.id = blocks.website 
       LEFT JOIN impressions 
         ON blocks.id = impressions.block 
WHERE  status = 1 
       AND blocks.active = '1' 
ORDER  BY impressions.number 
LIMIT  5 

If you wanted to sum of blocks.price you could just do

 SELECT SUM(lastblocks.price) 
   FROM (
    SELECT 
          price
    FROM   websites 
           LEFT JOIN blocks 
             ON websites.id = blocks.website 
           LEFT JOIN impressions 
             ON blocks.id = impressions.block 
    WHERE  status = 1 
           AND blocks.active = '1' 
    ORDER  BY impressions.number 
    LIMIT  5 ) lastblocks
﹎☆浅夏丿初晴 2024-11-12 08:52:55

要获取最后 5 条记录,这取决于您排序所依据的列。假设这是一个日期列,那么一个例子是:

SELECT MAX(n) FROM tbl ORDER BY datecol DESC limit 5;

To get the last five records, it depends on the column by which you are sorting. Let's assume this is a date column, so then an example would be:

SELECT MAX(n) FROM tbl ORDER BY datecol DESC limit 5;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文