如果在左连接中存在行功能,则应用

时间:2017-08-02 07:57:01

标签: mysql

我正在使用左连接组合3-6个表。以下是from子句

中代码的一些示例
document.getElementById("uploadFile").value

如果第二个表和第三个表上没有行,则不会显示第四个表中的数据等。我需要检查第二个表上的行是否存在。如果是这样,它将使用左连接第二个表,如果不是,它将在第三个表上使用左连接,依此类推。

3 个答案:

答案 0 :(得分:0)

我认为LEFT JOIN不会像你要求的那样工作,因为如果父表中没有条目,你将无法从孙中获取数据。

在我看来,你可以简单地将CASE与Count(不是最佳实践)一起使用,但它可以解决你的问题。

答案 1 :(得分:0)

您可以对这些SELECTJOIN所有问题(UNION}使用单独的DISTINCT查询到一个查询中,例如:

SELECT DISTINCT column_name
FROM (
 SELECT column_name
 FROM ip_ucp_01
  LEFT JOIN ip_lt_01 ON ip_ucp_01.time = ip_lt_01.time AND ip_ucp_01.date =   ip_lt_01.date
  LEFT JOIN ip_le ON ip_lt_01.time = ip_le.time AND ip_lt_01.date = ip_le.date;

  UNION

  SELECT column_name
  FROM ip_ucp_01
  LEFT JOIN ip_lmiv_01 ON ip_ucp_01.time = ip_lmiv_01.time AND ip_ucp_01.date =   ip_lmiv_01.date

) a;

答案 2 :(得分:0)

如果你刚加入第一张桌子怎么样?像这样:

     FROM ip_ucp_01
LEFT JOIN ip_lt_01 
       ON ip_ucp_01.time = ip_lt_01.time 
      AND ip_ucp_01.date = ip_lt_01.date
LEFT JOIN ip_le 
       ON ip_ucp_01.time = ip_le.time 
      AND ip_ucp_01.date = ip_le.date
LEFT JOIN ip_lmiv_01 
       ON ip_ucp_01.time = ip_lmiv_01.time 
      AND ip_ucp_01.date = ip_lmiv_01.date
LEFT JOIN ip_cwg 
       ON ip_ucp_01.time = ip_cwg.time 
      AND ip_ucp_01.date = ip_cwg.date
LEFT JOIN ip_mtu_01 
       ON ip_ucp_01.time = ip_mtu_01.time 
      AND ip_ucp_01.date = ip_mtu_01.date

因为你只是加入日期和时间而不是这样做你想做什么?