0

这是我用 mysql 查询 http://sqlfiddle.com/#!2/f2794/4制作的 sqlfiddle

当存在 = 0 时,它连续计算 10 天,但我需要添加第二个条件来计算存在 > 10 的情况。

例如

11 22 0 0 0 0 0 0 0 0 0 0 0 0 1

应该数 14

这是那个查询

select sum(count) total from (

SELECT COUNT(present) as count FROM (
SELECT
IF((q.present != 0), @rownum:=@rownum+1, @rownum:=@rownum) AS rownumber, @prevDate:=q.date, q.*
FROM (
SELECT
name
, date
, present
FROM
teacher, (SELECT @rownum:=0, @prevDate:='') vars
WHERE date BETWEEN '2013-07-01' AND '2013-07-31'
ORDER BY date, present
) q
) sq
GROUP BY present, rownumber
HAVING COUNT(*) >= 10

) d

所以,如果你能帮助我,请这样做:)

最好的问候米。

4

1 回答 1

0

我不太了解您的查询,但我认为只需更改 (q.Present != 0) 以合并附加测试即可解决您的问题:

SELECT sum(count) total from (
  SELECT COUNT(present) as count FROM (
    SELECT
      IF((q.present != 0 AND q.present <= 10), @rownum:=@rownum+1, @rownum:=@rownum) AS rownumber, @prevDate:=q.date, q.*
    FROM (
      SELECT
        name
        , date
        , present
      FROM
        teacher, (SELECT @rownum:=0, @prevDate:='') vars
      WHERE date BETWEEN '2013-07-01' AND '2013-07-31'
      ORDER BY date, present
    ) q
  ) sq
  GROUP BY present, rownumber
  HAVING COUNT(*) >= 10
) d
于 2013-09-25T23:45:54.123 回答