对avgs的两个Php sql查询得到相同的结果。 为什么?

Like i said up top, I have two queries right here.

$avg = "SELECT avg(CASE WHEN Lasttrade = 0 THEN NULL ELSE Lasttrade END) FROM $month
         ORDER BY ID DESC LIMIT 0, 25";


$avg = "SELECT avg(CASE WHEN Lasttrade = 0 THEN NULL ELSE Lasttrade END) FROM $month
         ORDER BY ID DESC LIMIT 0, 5";

I run them at seperate times, using

$result = $db->query($avg) or die ("avg t1 ");

but they pull the same results. I want to pull the avg of the last 5, and the last 25. But i get the same exact number. any thoughts?

LIMIT only limits the number of rows returned to you from your query. It does not limit the rows used in the query/calculations. You need to use a WHERE clause or subquery for that (depending on what you need exactly).

John pointed out the 'why' already, LIMIT acts on the result set, not the query process, but here is how you need to do this, limiting the records to be averaged via subquery:

    SELECT avg(CASE WHEN Lasttrade = 0 THEN NULL ELSE Lasttrade END) 
    FROM (SELECT *              FROM $month
          ORDER BY ID DESC 
          LIMIT 0, 25") AS sub;

Your query is returning one row because it is an aggregation query. The limit is the last logical step in query processing -- after aggregation and the order by clause.

Try using a subquery to limit the number of rows:

SELECT avg(CASE WHEN Lasttrade <> 0 THEN Lasttrade END)
FROM (select m.*
      from $month m
      ORDER BY ID DESC LIMIT 0, 25
     ) m