MYSQL行计数是否满足条件

时间:2014-02-15 15:51:23

标签: mysql

很抱歉,但我不确定如何说出这个问题,或者是否可能,但基本上我使用的是一个select语句,我想在其中显示一个列,显示符合条件的时间。例如

SELECT pageID, isHome, if(ishome = 1, 'count?', 0) AS Passed
FROM pages

我希望passed列显示类似这样的运行计数

PageID | ishome | passed
10031  | 0      | 0
10032  | 1      | 1
10033  | 1      | 2
10034  | 1      | 3

感谢您的帮助

4 个答案:

答案 0 :(得分:1)

尝试类似以下内容(未经测试)

select a.pageid, a.ishome, sum(b.ishome) passed 
from 
pages a join pages b
on a.pageid>= b.pageid
group by a.pageid, a.ishome
order by a.pageid

答案 1 :(得分:0)

    SELECT pageID, isHome,  count(*)  AS Passed 
    FROM pages
    where ishome = 1
    GROUP BY PageID 

DEMO

如果您想显示所有结果,请使用此

 SELECT pageID, isHome,  if(ishome = 1, count(*), 0)  AS Passed 
 FROM pages
 group by pageID,ishome ;

DEMO

答案 2 :(得分:0)

不确定,但我认为这是你想要的。

SELECT pageID, isHome, 
case when(ishome = 1)
  then count(*)
  else 0
end Passed FROM Table1
group by pageID;

sqlfiddle

如果您想要传递的页数

SELECT isHome, 
case when(ishome = 1)
  then count(*)
  else 0
end Passed FROM Table1
group by isHome;

sqlfiddle

答案 3 :(得分:0)

尝试这样的事情

SELECT pageID, isHome,
CASE isHome 
  WHEN 1 THEN SELECT COUNT(*) FROM pages p 
              WHERE p.isHome = 1 AND p.pageID <= pageID 
  ELSE 0 
END as passed
FROM pages
ORDER BY pageID ASC