mysql 过滤问题这是我的数据库表,我通过以下sql可以过滤数据select*from chat a whereoId(SELECT MAX(oId
mysql 过滤问题 这是我的数据库表,
我通过以下sql可以过滤数据
select * from chat a where oId=(SELECT MAX(oId) FROM chat b where a.sendId =b.sendId) 但是,我想让得出来的结果后边在增加一列,得出每一个sendId的数量,如下图
[最优解释] 谢谢,这个行不?
select * from chat a INNER JOIN ( SELECT sendid , COUNT(*) AS count FROM chat WHERE isread = 1 GROUP BY sendid ) AS x ON a.sendid = x.sendid where oId=(SELECT MAX(oId) FROM chat b where a.sendId =b.sendId AND b.isread=1)
另外,你是想只计算isread=1的数是吧? [其他解释] select a.*,x.count from chat a inner join (select sendid,count(*) as count from chat group by sendid) as x on a.sendid=x.sendid where oId=(SELECT MAX(oId) FROM chat b where a.sendId =b.sendId and b.isRead=1 ) 试试这个 [其他解释]
SELECT *,b.sendid FROM chat a,(SELECT COUNT(sendid)sendid,sendname FROM chat GROUP BY sendname )b WHERE oId = ( SELECT MAX(oId) FROM chat b WHERE a.sendId = b.sendId ) AND a.sendname=b.sendname
[其他解释] select a.*,x.count from chat a inner join (select sendid,count(*) as count from chat group by sendid) as x on a.sendid=x.sendid where oId=(SELECT MAX(oId) FROM chat b where a.sendId =b.sendId) [其他解释]
怎么给你这个sql语句加一个 where isRead=1 谢谢大神
[其他解释]
你这个语句重复字段好多 [其他解释] select a.*,x.count from chat a inner join (select sendid,count(*) as count from chat group by sendid) as x on a.sendid=x.sendid where oId=(SELECT MAX(oId) FROM chat b where a.sendId =b.sendId) and isRead=1 [其他解释] 我主要是不想造数据,你贴数据我还能正一些,图片太。。。 [其他解释]
后边加 and isRaed=1 结果是1条数据,本应该是3条数据 [其他解释]
SELECT a.* , x.count FROM chat a INNER JOIN ( SELECT sendid , COUNT(*) AS count FROM chat WHERE isread = 1 GROUP BY sendid ) AS x ON a.sendid = x.sendid WHERE oId = ( SELECT MAX(oId) FROM chat b WHERE a.sendId = b.sendId )
[其他解释]
是按照isread=1了,但是cout的结果是全部的,不对 [其他解释]
怎么把isRead=0的结果也查询出来了 [其他解释] 没有你的数据很难测
SELECT a.* , x.count FROM chat a INNER JOIN ( SELECT sendid , COUNT(*) AS count FROM chat WHERE isread = 1 GROUP BY sendid ) AS x ON a.sendid = x.sendid WHERE oId = ( SELECT MAX(oId) FROM chat b WHERE isread = 1 WHERE a.sendId = b.sendId )