计算最后10行中的匹配数量

时间:2016-04-06 23:49:26

标签: php mysql

我需要帮助来实现在结果的最后10行中我有多少状态= 1?

如果在最后10行中有3个状态,我需要输出3个。

SELECT * FROM results WHERE $position='$text' and make='$make' and status='1'

1 个答案:

答案 0 :(得分:3)

您需要使用子查询来获取最后10行,然后计算该子集中有多少status = 1

SELECT COUNT(*) AS count
FROM (SELECT status
      FROM results
      WHERE $position = '$text' AND make = '$make'
      ORDER BY id DESC
      LIMIT 10) AS last10
WHERE status = '1'

DEMO