MySQL计算另一列=值的行

时间:2011-10-07 03:42:55

标签: mysql count

我有一个While循环,在每行中显示COUNT p.songid(帖子后跟踪)。我真正需要的是根据trackDeleted=0而不是所有行来计算。

 SELECT u.username, u.id, u.score, s.genre, s.songid, s.songTitle, s.timeSubmitted, s.userid, s.insWanted, s.bounty, COUNT(p.songid)
 FROM  songs s
 LEFT JOIN users u ON u.id = s.userid
 LEFT JOIN posttracks p ON s.songid = p.songid
 WHERE paid=1 AND s.timeSubmitted >= ( CURDATE() - INTERVAL 60 DAY )
 GROUP BY s.timeSubmitted ASC
 LIMIT 25

我可以更新上述查询,还是必须在单独的查询中?

8 个答案:

答案 0 :(得分:1)

我不确定这是否适用于mySql,但在SQL中,您可以在连接期间进行过滤,如下所示:

SELECT u.username, u.id, u.score, s.genre, s.songid, s.songTitle, s.timeSubmitted, s.userid, s.insWanted, s.bounty, COUNT(p.songid)
FROM  songs s
LEFT JOIN users u ON u.id = s.userid
LEFT JOIN posttracks p ON s.songid = p.songid and trackDeleted=0
WHERE paid=1 AND s.timeSubmitted >= ( CURDATE() - INTERVAL 60 DAY )
GROUP BY s.timeSubmitted ASC
LIMIT 25

答案 1 :(得分:0)

这取决于trackDeleted列在哪个表中。

如果它存在于赛道后,则很简单

SELECT COUNT(p.songid) FROM posttracks p where p.trackDeleted=0

答案 2 :(得分:0)

trackDeleted = 0添加到您的WHERE子句:

...
WHERE paid=1 AND s.timeSubmitted >= ( CURDATE() - INTERVAL 60 DAY )
    AND trackDeleted=0
 --  ^^^ add this
GROUP BY s.timeSubmitted ASC
...

答案 3 :(得分:0)

试试这个......     您必须按p.songid分组。

 SELECT u.username, u.id, u.score, s.genre, s.songid, s.songTitle,
 s.timeSubmitted, s.userid, s.insWanted, s.bounty, COUNT(p.songid) FROM
 songs s LEFT JOIN users u ON u.id = s.userid LEFT JOIN posttracks p ON
 s.songid = p.songid WHERE paid=1 AND s.timeSubmitted >= ( CURDATE() -
 INTERVAL 60 DAY ) GROUP BY s.timeSubmitted ASC,p.songid LIMIT 25

答案 4 :(得分:0)

如果你要COUNT歌曲ID,你必须自己分组,试试这个。

 SELECT u.username, u.id, u.score, s.genre, s.songid, s.songTitle, s.timeSubmitted, s.userid, s.insWanted, s.bounty, COUNT(p.songid)
   FROM  songs s
   LEFT JOIN users u
     ON u.id = s.userid
   LEFT JOIN posttracks p
     ON s.songid = p.songid
  WHERE paid=1 
    AND s.timeSubmitted >= ( CURDATE() - INTERVAL 60 DAY )
    AND trackDeleted=0
  GROUP BY p.songid
  ORDER BY s.timeSubmitted
  LIMIT 25;

答案 5 :(得分:0)

将测试置于连接条件中:

LEFT JOIN posttracks p
    ON s.songid = p.songid
    AND trackDeleted = 0

或者将COUNT(p.songid)更改为SUM:

SUM(CASE WHEN (p.songid IS NOT NULL AND trackDeleted = 0) THEN 1 ELSE 0 END)

答案 6 :(得分:0)

万一有人绊倒了,就像我一样,正确答案可能就像:

SELECT u.username, u.id, u.score, s.genre, s.songid, s.songTitle, s.timeSubmitted, s.userid, s.insWanted, s.bounty, COUNT(IF(p.trackDeleted=0, 1, NULL)) as not_deleted_count
     FROM  songs s
     LEFT JOIN users u
     ON u.id = s.userid
     LEFT JOIN posttracks p
     ON s.songid = p.songid
     WHERE paid=1 AND s.timeSubmitted >= ( CURDATE() - INTERVAL 60 DAY )
     GROUP BY s.timeSubmitted ASC
     LIMIT 25

密钥为COUNT(IF(p.trackDeleted=0, 1, NULL)) as not_deleted_count,它将查看分组中的所有曲目,并仅计算未删除的曲目。

此处的其他答案都误将OP误认为只想选择未删除的曲目,而他想要的是计算每个分组中未删除的曲目数。

答案 7 :(得分:-1)

试试这个。

DECLARE @trackCount int,
SELECT u.username, u.id, u.score, s.genre, s.songid, s.songTitle, s.timeSubmitted, s.userid, s.insWanted, s.bounty,
 FROM songs s 
 LEFT JOIN users u ON u.id = s.userid 
 LEFT JOIN posttracks p ON s.songid = p.songid 
 WHERE paid=1 
 AND s.timeSubmitted >= ( CURDATE() - INTERVAL 60 DAY)
 GROUP BY s.timeSubmitted ASC
 LIMIT 25

 SELECT COUNT(p.songid) INTO @TrackCount
 FROM posttracks p
 WHERE p.trackDeleted = 0

尝试将此添加到您的代码中。如果你需要显示trackDeleted = 0的轨道数,只需访问局部变量@trackcount。