简体   繁体   English

php mysql asc / desc命令

[英]php mysql asc/desc order

TABLE: 表:

**timeslot**:
----------
id_timeslot   times
1             09:00
2             09:30
3             10:00
4             10:30
5             11:00

**bookslot**
id   id_timeslot     date        b_ref
-------------------------------------------
1    2               2010-02-22  001 
2    3               2010-02-22  001
3    4               2010-02-22  001
4    5               2010-02-22  001
5    2               2010-02-25  002
6    3               2010-02-27  003
7    4               2010-02-27  003
8    5               2010-02-27  003

PHP PHP

$q = $mysqli->query("SELECT * FROM bookslot  
LEFT JOIN timeslot ON bookslot.id_timeslot = timeslot.id_timeslot   
WHERE bookslot.status = 1 
GROUP BY bookslot.b_ref  
ORDER BY bookslot.date ASC, bookslot.id_timeslot ASC LIMIT 20");

HTML RESULT: HTML结果:

DATE         TIMES  
2010-02-22   10:30
2010-02-25   09:30
2010-02-27   11:00

anyone notice that on the table result. 有人注意到桌子上的结果。 the times is incorrect order? 时间是不正确的顺序?
i changed another way round with ASC / DESC, and still the times showing the last id_timeslot? 我用ASC / DESC改变了另一种方式,仍然显示最后一个id_timeslot?

EXPECTED RESULT: 预期结果:

DATE         TIMES  
2010-02-22   09:30
2010-02-25   09:30
2010-02-27   10:00

Your GROUP BY bookslot.b_ref is grouping the records, so you're only seeing the last time in each case. 您的GROUP BY bookslot.b_ref正在对记录进行分组,因此您只能查看每种情况下的最后一次。

Try using 尝试使用

SELECT date, time, MIN(bookslot.id_timeslot)
FROM bookslot  
LEFT JOIN timeslot ON bookslot.id_timeslot = timeslot.id_timeslot   
WHERE bookslot.status = 1 
GROUP BY bookslot.b_ref  
ORDER BY bookslot.date ASC, bookslot.id_timeslot ASC LIMIT 20

While your SQL is syntactically correct but it will produce unexpected results. 虽然您的SQL在语法上是正确的,但它会产生意外的结果。

Normally, the columns that you SELECT must be specified in the GROUP BY clause or should be enclosed inside an aggregate function. 通常, SELECT的列必须在GROUP BY子句中指定,或者应该包含在聚合函数中。 Otherwise, MySQL will determine, at its own discretion, which records to eliminate in the GROUP BY operation. 否则,MySQL将自行决定在GROUP BY操作中消除哪些记录。 The ORDER BY does not matter because it is applied after the GROUP BY operation. ORDER BY无关紧要,因为它是 GROUP BY操作之后应用的。 You should better revise your query like this: 你应该更好地修改你的查询:

SELECT b_ref, MIN(ADDTIME(date, times)) AS complete_datetime
FROM bookslot
LEFT JOIN timeslot ON bookslot.id_timeslot = timeslot.id_timeslot
WHERE bookslot.status = 1
GROUP BY bookslot.b_ref
ORDER BY bookslot.date, bookslot.id_timeslot

Since the goal appears to be about collecting the earliest timeslots for each bookslot then it's required to narrow the results with MIN 由于目标似乎是为每个书架收集最早的时间段,因此需要使用MIN缩小结果范围。

SELECT b.id, b.id_timeslot, b.date, MIN(`date`) , t.times
FROM bookslot b
LEFT JOIN timeslot t ON b.id_timeslot = t.id_timeslot
GROUP BY b.b_ref

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM