簡體   English   中英

MySQL查詢加入條件不起作用

[英]Mysql query to join condition not working

我有一種情況,我需要通過table_3下面的table_1table_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添加一個表,並檢查table_4是否有記錄,然后table_4進行標記,因此我將查詢寫入左連接table_4為:

查詢2

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_4table_5 table_4在一起,以便將結果限制為特定用戶的組。 所以我寫查詢為:

查詢3

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的情況下獲得的table_4 (公共記錄)中沒有記錄,則此查詢不會重現結果。

簡而言之,我不希望將查詢1的結果與table_4table_5內部table_4 table_5

任何幫助,將不勝感激。

編輯:

還忘了提到我正在使用Doctrine Query Builder。 因此,我沒有選擇子查詢的方法。 我簡化了此查詢以避免顯示Doctrine QB實現。

我不確定,但這應該可以工作,未經測試。

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離開聯接table_5這樣您將只獲得用戶1512數據以及不在table5和table4中的數據。

希望能幫助到你。

一種方法是創建一個內聯視圖,該視圖首先將表4和5聯接在一起,然后再聯接回您的基本集...我將結果別名為“ B”,然后將外部選擇更新為使用B.ID並進行了更新。左連接的ON table_6.id = B.bwic_id的打開條件

之所以可行,是因為引擎必須先解析子選擇,然后在左聯接發生之前生成結果。 使用這種方法,由於左聯接之后的內部聯接,使左聯接變得無效。

可以在聯接周圍添加MsACCESS()中的,以指示聯接操作的順序。 mySQL也可能支持此功能,我只是沒有使用過它。

理想情況下,您將*替換為您對表4和5感興趣的字段列表。如果表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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM