1

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.

OMG Ponies
  • 325,700
  • 82
  • 523
  • 502
Brandon
  • 11
  • 2

2 Answers2

2

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
Conrad Frix
  • 51,984
  • 12
  • 96
  • 155
1

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;
AJ.
  • 27,586
  • 18
  • 84
  • 94