左侧不包括JOIN不起作用

时间:2017-05-23 04:59:45

标签: mysql

我有两个SQL查询。

Select * from users (Returns 74 rows)

id      firstname       lastname
1       vignesh         km
2       hariram         v
3       vijesh          g
.
.
74      ram             k


Select * from details where time between '2017-05-11 00:00:00' AND '2017-05-11 23:59:59' (Returns 5 row).


id      userid      logindate   
57      1           2017-05-11 09:07:23
63      3           2017-05-11 09:04:57
65      6           2017-05-11 09:14:45
83      13          2017-05-11 09:26:43
65      74          2017-05-11 09:11:53

我试过

SELECT *
FROM details dt LEFT JOIN users us ON us.id = dt.userid
WHERE dt.logindate BETWEEN '2017-05-11 00:00:00' AND '2017-05-11 23:59:59' OR dt.userid IS NULL
GROUP BY dt.userid; (6 rows returning - in that one row contains full of null)

SELECT *
FROM details dt LEFT JOIN users us ON us.id = dt.userid
WHERE dt.logindate BETWEEN '2017-05-11 00:00:00' AND '2017-05-11 23:59:59'
GROUP BY dt.userid; (5 rows returning)


id      userid      logindate               firstname       lastname
57      1           2017-05-11 09:07:23     vignesh         km
63      3           2017-05-11 09:04:57     vijesh          g
65      6           2017-05-11 09:14:45     ..              ..
83      13          2017-05-11 09:26:43     ..              ..
65      74          2017-05-11 09:11:53     ram             k

两者都不起作用。但我需要在第一个查询结果中可用的69行,而在第二个查询结果中不可用。

3 个答案:

答案 0 :(得分:2)

虽然我不知道您使用Gradle 3.3的原因,但如果您想要提取group by中不存在的users条记录,可以试试这个:

details

或使用select * from users u left join details d on u.id = d.userid and d.logindate BETWEEN '2017-05-11 00:00:00' AND '2017-05-11 23:59:59' where d.id is null

not exists

答案 1 :(得分:-1)

Select * from users us 
left join  details  dt on us.id=dt.userid
WHERE dt.logindate NOT BETWEEN '2017-04-26 00:00:00' AND
'2017-04-26 23:59:59' GROUP BY dt.userid

答案 2 :(得分:-1)

不要使用GROUP BY

SELECT * FROM details dt 
LEFT JOIN users us ON us.id = dt.userid 
WHERE dt.userid IS NOT NULL AND dt.logindate 
BETWEEN '2017-05-11 00:00:00' AND '2017-05-11 23:59:59