我的表xdatelist包含:
+---------+
xDateList
+---------+
2018-11-01
2018-11-02
2018-11-03
2018-11-04
2018-11-05
还有扫描表
--------------------------------------
ID Name ScanDate Code
--------------------------------------
1 John 2018-11-02 07:00:00 IN
1 John 2018-11-02 10:00:00 OUT
1 John 2018-11-04 08:00:00 IN
1 John 2018-11-04 12:00:00 OUT
我试过这个,但它不能显示xdatelist上的所有记录,它只显示scanlog表上的记录
select xDateList.date,
scanlog.name,
MIN(scanlog.scandate) AS `IN`,
MAX(scanlog.scandate) AS `OUT`
from scanlog
left JOIN xDateList ON xDateList.date = date(scanlog.scandate)
where scanlog.id='1'
GROUP BY DATE(scanlog.scandate)
我想要这样的结果
--------------------------------------------
Date ID Name In Out
--------------------------------------------
2018-11-01 1 John
2018-11-02 1 John 07:00:00 10:00:00
2018-11-03 1 John
2018-11-04 1 John 08:00:00 12:00:00
2018-11-05 1 John
谢谢你帮助我
1条答案
按热度按时间7fhtutme1#
您需要更改表的顺序
LEFT JOIN
. 始终记住,为了考虑特定表中的所有行;该特定表应该是联接中最左边的表。另外,每当执行左联接时,应该在
ON
条款;否则where子句中的条件可以有效地将其转换为内部连接。另外,在这种情况下
GROUP BY
应该在xDateList.date
显示对应于xDateList.date
价值观。而且,我们需要确保SELECT
列表中指定GROUP BY
条款。do check:在mysql中执行查询时,只与\u full \u group \u by相关的错误结果
db fiddle视图