mysql group by last record也在最后一条记录中找到备注

时间:2016-03-28 16:08:19

标签: php mysql

抱歉错过了我的表格和数据:

tablename = tbreport

comcode,user_id,comname,remark,date

0010066,32,Wicam,NotInterest,2014-02-01 0010066,32,Wicam,Discusing,2014-02-05 0010066,32,Wicam,Interest,2014-02-08 0010066,32,Wicam,Pending,2014-02-21 0010596,32,United,Interest,2014-02-08 0010606,32,SEACAM,NotInterest,2014-03-08 0040025,16,SkyView,Interest,2014-03-17

我使用下面的查询:

$ uer_id =' $ _ GET [USER_ID]&#39 ;; $备注=' $ _ GET [备注]&#39 ;; $ query = $ link->查询(" SELECT * FROM(SELECT * FROM tbreport,其中user_id =' $ uid'按日期排序DESC)tbreport GROUP BY comcode");

结果显示最后一条记录:

comcode,user_id,comname,remark,date

0010066,32,Wicam,Pending,2014-02-21 0010596,32,United,Interest,2014-02-08
0010606,32,SEACAM,NotInterest,2014-03-08 0040025,32,SkyView,Interest,2014-03-17

我需要的结果是什么

当$ remark ='兴趣'应该如下所示

comcode,user_id,comname,remark,date

0010596,32,United,Interest,2014-02-08
0040025,32,SkyView,Interest,2014-03-17

当$ remark ='待定'应该如下所示

comcode,user_id,comname,remark,date

0010066,32,Wicam,Pending,2014-02-21

那我该怎么办呢? 谢谢,如果有任何疑问 萨利

1 个答案:

答案 0 :(得分:1)

SELECT m1.* FROM tbreport m1 LEFT JOIN tbreport m2 
ON (m1. comname = m2. comname AND m1.date < m2.date) 
WHERE m2.date IS NULL and m1.remark='$remark';