我使用php编程为Leave Management项目从MySQL数据库生成报告。
// mysql query to fetch the report
SELECT * FROM vrv_intranet_leaverequest
WHERE
((fromdate >='2014-03-10' and fromdate <='2014-03-20')
||
(todate >='2014-03-10' and todate <='2014-03-20'))
我从此查询中获得了10个订单项
Id Name 01/03 02/03 03/04 04/04 05/05
25 Suresh L P P P P
25 Suresh P L P P P
28 Balaji L L P P P
29 Kumar P P L L P
30 Sri P L P P P
31 Ganesh L L P P P
45 Alex P P P L L
获得第25个ID是两次在表中,因为在我的数据库中我有记录。但我想在单行显示在前面。
第25个id在第1和第2个离开,所以它显示为两个记录。但我希望在单个记录中看起来像下面这样:
Id Name 01/03 02/03 03/04 04/04 05/05
25 Suresh L L P P P
答案 0 :(得分:2)
使用GROUP BY
收集具有相同ID的所有行,并使用MIN()
选择每列中的最小值:
SELECT Id, Name,
MIN(`01/03`) AS `01/03`,
MIN(`02/03`) AS `02/03`,
MIN(`03/04`) AS `03/04`,
MIN(`04/04`) AS `04/04`,
MIN(`05/05`) AS `05/05`
FROM vrv_intranet_leaverequest
WHERE ((fromdate >='2014-03-10' and fromdate <='2014-03-20')
||
(todate >='2014-03-10' and todate <='2014-03-20'))
GROUP BY Id
答案 1 :(得分:0)
我假设Id是主键
SELECT * FROM vrv_intranet_leaverequest WHERE
((fromdate >='2014-03-10' and fromdate <='2014-03-20') ||
(todate >='2014-03-10' and todate <='2014-03-20')) GROUP BY Id