繁体   English   中英

php mysql连接4个表可能的空值

[英]php mysql joining 4 tables with possible null values

我有以下sql查询数据库并从4个不同的表中提取信息。

 SELECT d.item AS day, m.item as month, y.item as yr, l.item as local
 FROM date_day d
 JOIN date_month m ON d.recid=m.recid
 JOIN date_year y ON d.recid=y.recid
 JOIN location l ON d.recid=l.recid
 WHERE d.recid='cneta0ld00s6'

可能一个或多个表可能为空且不包含值。 特别是在日期字段上。 以上任何一个是空的还是不存在的,会导致整个事情失败? 我特别担心date_day,因为我知道mysql没有FUll JOIN。

有什么想法吗?

我觉得:

SELECT d.item AS day, m.item as month, y.item as yr, l.item as local
 FROM date_day d
 LEFT JOIN date_month m ON d.recid=m.recid
 LEFT JOIN date_year y ON d.recid=y.recid
 LEFT JOIN location l ON d.recid=l.recid
 WHERE d.recid='cneta0ld00s6'

会做你想要的

为了确保我总是得到表中的数据,即使它是null,我在另一个表中添加了一个主表,一个总是包含recid,表是照片。 这样我确保查询将始终返回一些东西,即使它有很多空值。 这意味着我也可以使用左连接。

 SELECT p.photo_file, d.item AS day, m.item as month, y.item as yr, l.item as local
  FROM photo p
 LEFT JOIN date_day d ON p.recid=d.recid
 LEFT JOIN date_month m ON p.recid=m.recid
 LEFT JOIN date_year y ON p.recid=y.recid
 LEFT JOIN location l ON p.recid=l.recid
 WHERE p.recid='paul'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM