这是我的数据库表,
我通过以下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 谢谢大神