php mysql asc/desc顺序

标签 php mysql sql date group-by

表格:

**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

$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 结果:

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

任何人都注意到表上的结果。时间顺序不对?
我用 ASC/DESC 改变了另一种方式,仍然显示最后一个 id_timeslot 的时间?

预期结果:

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

最佳答案

您的 GROUP BY bookslot.b_ref 正在对记录进行分组,因此您只能看到每种情况下的最后一次。

尝试使用

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

关于php mysql asc/desc顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5078381/

相关文章:

sql - 删除特定列为空的行

php - 我想创建一个表,但它给我错误 1046

php - Windows 上的 PHP 7.1 x64 NTS 缺少 MongoDB 库

MySQL 根据另一列的值对一列进行分组

c# - 设计数据驱动的逻辑系统

php - WordPress MySql 插入

php - "error": "InvalidRegistration" while sending notification from PHP

php - 解析位置41时间字符串失败(i) : Double timezone specification

php - 从数组中查找与 cookie 不匹配的值

PHP准备和执行