Mysql查询加入条件不起作用

时间:2015-08-13 13:37:49

标签: mysql sql join

我有一个案例,我需要通过加入table_1获取数据,table_2table_3table_6下面是代表我需要放置的所有条件的查询以下查询工作正常并获得完美数据

  

查询1

SELECT   table_1.id                                                   
AS id1,   table_1.deal_id                                             
AS deal_id5,   CONVERT_TZ(table_1.due_date, 'UTC', 'EST')             
AS sclr20,   table_2.deal_name                                        
AS deal_name22 FROM  table_1   INNER JOIN  table_2 ON (table_1.deal_id
= table_2.deal_id)   INNER JOIN  table_3 ON (table_1.id = table_3.child_bwic_id)   INNER JOIN  table_6 ON (table_6.id =
table_3.bwic_id)

WHERE table_6.data_source_id <> 2
      AND table_1.status IN (1, 2, 3)
      AND DATE(table_1.due_date) = '2015-08-13'

GROUP BY table_1.deal_id, sclr20 ORDER BY sclr20 ASC, id1 ASC;

现在由于某些要求,我需要再添加一个表来条件table_4,并检查table_4中是否有记录然后标记它,以便我向左连接{{1}写入查询as:

  

查询2

table_4

这个也很好并且给出了正确的结果集

要进一步深入挖掘结果,我需要将SELECT table_1.id AS id1, table_1.deal_id AS deal_id5, CONVERT_TZ(table_1.due_date, 'UTC', 'EST') AS sclr20, table_2.deal_name AS deal_name22, if(table_4.id IS NULL, 'public', 'private') AS sclr51 FROM child_bwic table_1 INNER JOIN deal table_2 ON (table_1.deal_id = table_2.deal_id) INNER JOIN bwic_child_association table_3 ON (table_1.id = table_3.child_bwic_id) INNER JOIN bwic table_6 ON (table_6.id = table_3.bwic_id) LEFT JOIN bwic_private_group_association table_4 ON table_6.id = table_4.bwic_id WHERE table_6.data_source_id <> 2 AND table_1.status IN (1, 2, 3) AND DATE(table_1.due_date) = '2015-08-13' GROUP BY table_1.deal_id, sclr20 ORDER BY sclr20 ASC, id1 ASC; 加入table_4,以便我可以将结果限制为特定用户的群组。所以我写了一个查询:

  

查询3

table_5

但是如果在SELECT table_1.id AS id1, table_1.deal_id AS deal_id5, CONVERT_TZ(table_1.due_date, 'UTC', 'EST') AS sclr20, table_2.deal_name AS deal_name22, if(table_4.id IS NULL, 'public', 'private') AS sclr51 FROM child_bwic table_1 INNER JOIN deal table_2 ON (table_1.deal_id = table_2.deal_id) INNER JOIN bwic_child_association table_3 ON (table_1.id = table_3.child_bwic_id) INNER JOIN bwic table_6 ON (table_6.id = table_3.bwic_id) LEFT JOIN bwic_private_group_association table_4 ON table_6.id = table_4.bwic_id JOIN bwic_private_group_user_association table_5 ON table_4.group_id = table_5.group_id AND table_5.user_id = 1512 WHERE table_6.data_source_id <> 2 AND table_1.status IN (1, 2, 3) AND DATE(table_1.due_date) = '2015-08-13' GROUP BY table_1.deal_id, sclr20 ORDER BY sclr20 ASC, id1 ASC; (公共记录)中没有记录,那么这个查询就不会产生结果。在查询2的情况下我会得到这样的记录。

我简短地说我不想将查询1的结果与table_4table_4的内联接结合起来。

任何帮助都将不胜感激。

  

编辑:

还忘了提到我正在使用Doctrine Query Builder。所以我没有子查询选项。我已简化此查询以避免显示Doctrine QB实现。

2 个答案:

答案 0 :(得分:1)

我不确定但这应该有效,而不是经过测试。

SELECT

  table_1.id
    AS id1,
  table_1.deal_id
    AS deal_id5,
  CONVERT_TZ(table_1.due_date, 'UTC', 'EST')
    AS sclr20,
  table_2.deal_name
    AS deal_name22,
  if(table_4.id IS NULL, 'public', 'private')
    AS sclr51
FROM child_bwic table_1
  INNER JOIN deal table_2 ON (table_1.deal_id = table_2.deal_id)
  INNER JOIN bwic_child_association table_3 ON (table_1.id = table_3.child_bwic_id)
  INNER JOIN bwic table_6 ON (table_6.id = table_3.bwic_id)


  LEFT JOIN bwic_private_group_association table_4 ON table_6.id = table_4.bwic_id
  left JOIN bwic_private_group_user_association table_5 ON table_4.group_id = table_5.group_id

WHERE table_6.data_source_id <> 2
      AND table_1.status IN (1, 2, 3)
      AND DATE(table_1.due_date) >= '2015-08-19'
      and (table_5.user_id = 1512 or table_5.user_id is NULL )
GROUP BY table_1.deal_id, sclr20
ORDER BY sclr20 ASC, id1 ASC;

我做的是离开加入table_5保持条件table_5.user_id = 1512 or table_5.user_id is NULL,这样您将只获得用户1512数据以及不在table5和table4中的数据。

希望它有所帮助。

答案 1 :(得分:0)

这样做的一种方法是创建一个连接表4和表4的内联视图。 5然后首先加入到你的基础集...我做了这个结果,结果为&#34; B&#34;然后更新外部选择以使用B.ID并将开启标准更新为ON table_6.id = B.bwic_id左连接

为什么这样做是因为引擎必须首先解析子选择,然后在左连接发生之前生成结果。由于左连接后的内部连接,左边连接的方法会被取消。

可以在连接周围添加MsACCESS()中的

以指示连接操作的顺序。 mySQL也可能支持这一点,我只是没有使用它。

理想情况下,您可以将*替换为您对表4和表4感兴趣的字段列表。如果表4和表4所示,这将是必需的。 5都具有外部选择中引用的ID字段。

SELECT
  table_1.id AS id1,   
  table_1.deal_id AS deal_id5,   
  CONVERT_TZ(table_1.due_date, 'UTC', 'EST') AS sclr20,   
  table_2.deal_name AS deal_name22,   
  if(B.id IS NULL, 'public', 'private') AS sclr51 

FROM child_bwic table_1   
INNER JOIN deal table_2 
 ON table_1.deal_id = table_2.deal_id
INNER JOIN bwic_child_association table_3 
  ON table_1.id = table_3.child_bwic_id
INNER JOIN bwic table_6 
  ON (table_6.id = table_3.bwic_id)
LEFT JOIN 
 (SELECT * 
  FROM bwic_private_group_association table_4 
  INNER JOIN bwic_private_group_user_association table_5 
    ON table_4.group_id = table_5.group_id 
   AND table_5.user_id = 1512) B
  ON table_6.id = B.bwic_id   

WHERE table_6.data_source_id <> 2
      AND table_1.status IN (1, 2, 3)
      AND DATE(table_1.due_date) = '2015-08-13'
GROUP BY table_1.deal_id, sclr20 ORDER BY sclr20 ASC, id1 ASC;