我有这三个表:学生student_id 012345name Leeclass 5Agender Malenohp 011-1111111笔记本电脑idborrow 5student_id 012345 no_laptop LP12345lend_date 01/08/2019pass_date NULLsend_date 01/11/2019 经过idborrow 5student_id 012345no_laptop LP12345lend_date 01/08/2019 pass_date 01/08/2019 send_date 01/11/2019我想加入统计表(如果老师不同意):student_id 012345 name Lee class 5A gender Male nohp 011-1111111 idborrow 5 no_latop LP12345 lend_date 01/08/2019pass_date NULL send_date 01/11/2019如果老师批准他,就会是这样:student_id 012345 name Lee class 5A gender Male nohp 011-1111111 idborrow 5 no_latop LP12345 lend_date 01/08/2019pass_date 01/08/2019 send_date 01/11/2019我使用编码:$query=”select * from student inner join book on student.student_id=book.student_id”但是,它只显示 pass_date 为空的表。如果我使用:$query=”select * from student inner join book on student.student_id=book.student_id inner join pass on book.student_id=pass.student_id”它只显示它是否在传递表上有数据,但如果传递表为空则不显示。
3 回答
慕运维8079593
TA贡献1876条经验 获得超5个赞
这是因为,您正在使用INNER JOIN
. 请阅读这篇文章。您应该使用LEFT JOIN
或FULL OUTER JOIN
,具体取决于您想要数据的准确程度。
跃然一笑
TA贡献1826条经验 获得超6个赞
您必须在作为主表(承载大部分数据)的表上使用左连接,就像您的情况一样pass
。这意味着当pass left join...other tables
完成时,通过表的数据既匹配又不匹配的数据将是输出。
然而,
Inner Join 给出只与其他表数据匹配的数据。pass innerjoin other tables
将只来自两个表的匹配数据,但没有不匹配的数据(当您应用左连接时,它会给出左表的不匹配数据+两个表的匹配数据,反之亦然)
- 3 回答
- 0 关注
- 146 浏览
添加回答
举报
0/150
提交
取消