我知道MySQL可以进行简单的连接,但是当涉及到任何高级时,我都缺乏知识。我知道我可以删除PHP中的行,但我相信正确的查询更清晰。
我目前正在做的是:
查询:
SELECT DAYOFWEEK(r.`date`) AS weekNumber, r.`date`AS scoreDate, qf.target, r.totalScore FROM results;
结果:
weekNumber, scoreDate, target, totalScore
4, '2012-02-29', 85, 100
3, '2012-03-06', 85, 100
3, '2012-03-06', 85, 75
3, '2012-03-06', 85, 42
是否可以:
我想从查询中获得的结果是:
weekNumber, scoreDate, target, totalScore
4, '2012-02-29 to 2012-02-29', 85, 100
3, '2012-03-06 to 2012-03-06', 85, 72.3
有人可以帮我创建查询或告诉我如何生成我正在寻找的结果吗?
谢谢!
答案 0 :(得分:1)
SELECT q1, AVG(q2) as target, max(q3) as `end`, MIN(q3) as `start` from q GROUP BY q1
上面的查询应该做你想要的。只需更改你的字段q1 - q3。和你一起表名。
结果:
________________________________________
| q1 | target | end | start |
----------------------------------------
| 3 | 8.0000 | 2012-03-09 | 2012-03-08|
----------------------------------------
| 4 | 2.0000 | 2012-03-07 | 2012-03-07|
数据库:
________________________
| q1 | q2 | q 3 |
------------------------
| 3 | 7 | 2012-03-09 |
| 3 | 9 | 2012-03-08 |
| 4 | 2 | 2012-03-07 |