mysql 生成隨機時間 mysql 統計數量並根據最後面一個數據的時間排序

update czj_ask set createTime=concat('2016-',floor(6+rand()*2),'-',floor(11+rand()*10),' ' ,floor(10+rand()*10),':',floor(10+rand()*49),':',floor(10+rand()*49)) where createTime is NULLapp

select * from ( select t.userId,max(createTime) dd,count(*) as msgNum from czj_wz_app_data t where readed =0 GROUP BY userId)b order by b.dd desccode

若是要統計兩個數據 這樣就行 COUNT( CASE WHEN readed =1 THEN 1 ELSE NULL END ) AS msgNum1, COUNT( CASE WHEN readed =0 THEN 1 ELSE NULL END ) AS msgNum2date

```

SELECT b.userId, b.msgNum, c.trueName, c.headPhotoPath FROM ( SELECT t.userId, max(createTime) dd, COUNT( CASE WHEN readed =1 THEN 1 ELSE NULL END ) AS msgNum1, COUNT( CASE WHEN readed =0 THEN 1 ELSE NULL END ) AS msgNum2 FROM czj_wz_app_data t GROUP BY userId ) b LEFT JOIN czj_user c ON c.id = b.userId ORDER BY b.dd DESCselect

相關文章
相關標籤/搜索