0

在这里,我试图在Comment 列中获取字符串的中间值( log in ),然后获取每个用户的计数。但我认为这不是正确的做法。你们还有什么办法吗……!

user#   user ID Comments
101     user 1  ***log in*** : shera
101     user 1  ***log in*** : Mark
102     user 2  ***log out***
105     user 5  ***log in*** : Mark
104     user 4  ***ideal***
103     user 3  ***ideal***
101     user 1  ***ideal***
102     user 2  ***log out*** : since 5 mnt
105     user 5  ***log in*** : Mark
104     user 4  ***ideal***
103     user 3  ***log out***

查询需要设计用于:

         log in ideal   log out
user 1    2       0        3
user 2    0       2        5
user 5    0       2        1
          ------------------
Total      2      4        9

谢谢考虑。周杰伦

4

1 回答 1

0

不确定这是最优化的方式,但如果您的表名是comments(或根据需要在下面调整):

SELECT user, IFNULL(login,0) as login, IFNULL(ideal,0) as ideal,
       IFNULL(logout,0) as logout
FROM (
 SELECT userid, user
 FROM comments
 GROUP BY userid
) c
LEFT JOIN (
 SELECT userid, count(userid) AS login
 FROM comments
 WHERE comment LIKE '%log in%'
 GROUP BY userid
) c1 ON c.userid=c1.userid
LEFT JOIN (
 SELECT userid, count(userid) AS logout
 FROM comments
 WHERE comment LIKE '%log out%'
 GROUP BY userid
) c2 ON c.userid=c2.userid
LEFT JOIN (
 SELECT userid, count(userid) AS ideal
 FROM comments
 WHERE comment LIKE '%ideal%'
 GROUP BY userid
) c3 ON c.userid=c3.userid
于 2012-08-23T15:52:12.663 回答