社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  DATABASE

MYSQL在id条件为的左连接上不显示结果

MUHAMMAD MESUM • 3 年前 • 1401 次点击  

我有两张桌子,一张叫做附件,用来存储打卡和打卡记录,另一张是日历,我有所有的日期 附件: Attandances

日历: enter image description here

我想获取员工未在我的查询中输入的月份日期,如下所示:

select a.employeeID, c.cdate
from calendar c
    left join attandances a on (c.cdate = a.date)
where a.date is null
and a.employeeID='1'
and c.cdate between '2021-12-01' and '2021-12-31'

但当我执行时,它会返回0行。有什么建议我遗漏了什么吗?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/128988
 
1401 次点击  
文章 [ 3 ]  |  最新文章 3 年前
B.Quinn
Reply   •   1 楼
B.Quinn    3 年前

我想你在哪里

from calender c

应该是

from calender

而且

left join attandances a

应该是

left join attandances on calender.cdate = attandances.date
Leandro Curbelo
Reply   •   2 楼
Leandro Curbelo    3 年前

我想接收一些数据加载脚本,但由于您提供的图像中的可视化,查询将永远不会返回结果。

你在做一件事 JOIN 按列在两个表之间 (c.cdate = a.date) 然后在 WHERE 声明有两个相反的条件,一方面 a.date is null 另外呢 c.cdate between '2021-12-01' and '2021-12-31' ,请记住,在制作 参加 ,表关联的列的行为应该相同,因此不能在它们的值之间设置相反的条件。

在我看来,您试图得到的查询与此类似:

select DISTINCT calendar.cdate
from calendar
where calendar.date NOT IN (
        SELECT attandances.date
        FROM attandances
        WHERE attandances.employeeID = 1
    ) AND calendar.cdate between '2021-12-01' and '2021-12-31'
ysth
Reply   •   3 楼
ysth    3 年前
where a.date is null
and a.employeeID='1'

这些不可能都是真的。如果左连接未找到任何记录,date将为null,employeeID也将为null。如果确实找到记录,则日期将不为空。在我看来,您只是想将employeeID条件移动到您的join子句中:

from calendar c
    left join attandances a on c.cdate = a.date and a.employeeID='1'
where a.date is null
and c.cdate between '2021-12-01' and '2021-12-31'