2014-07-02 53 views
0

當我運行下面的MySQL語句中,我得到127行返回之間IDS:尋找失蹤MySQL表

SELECT t.strat_id, t.candle_id, t.spot_entry, t.spot_close, t.f,t.fa,t.b,t.we,t.direct,t.date_created,t.date_closed,t.notes,t.results 
      FROM cp_user_trades AS t 
      INNER JOIN candles2 AS c 
       ON t.candle_id = c.id 
      WHERE t.user_login = "user" 
      AND t.active=0 
      AND t.deleted=0 
      ORDER BY t.date_closed DESC 

當我刪除了INNER JOIN我得到131行返回:

SELECT t.strat_id, t.candle_id, t.spot_entry, t.spot_close, t.f,t.fa,t.b,t.we,t.direct,t.date_created,t.date_closed,t.notes,t.results 
      FROM cp_user_trades AS t 
      WHERE t.user_login = "user" 
      AND t.active=0 
      AND t.deleted=0 
      ORDER BY t.date_closed DESC 

我怎樣才能SELECT4行不會在第一條語句中返回?

謝謝!

回答

2

假設c.ID對於蠟燭2表的主鍵:

使用左聯接,並添加其中c.Id爲空

+0

完美,謝謝。將接受5。 – themerlinproject

1

要以表之間發現的ID,我用union allgroup by

select in_cut, in_c2, count(*) as cnt, min(candle_id), max(candle_id) 
from (select candle_id, sum(in_cut) as in_cut, sum(in_c2) as in_c2 
     from ((select candle_id, 1 as in_cut, 0 as in_c2 
      from cp_user_trades 
      ) union all 
      (select id, 0, 1 
      from candles2 
      ) 
      ) cc 
     group by candle_id 
    ) c 
group by in_cut, in_c2; 

這給你在兩個表中的ids的三種可能性(第一,第二和兩個)。它顯示了兩個表中的id是否重複,並給出了id的示例。

0

我的建議是使用第二個查詢返回131行,並使子查詢不包含其他127個查找到的子查詢。

SELECT t.strat_id, t.candle_id, t.spot_entry, t.spot_close, t.f,t.fa,t.b,t.we,t.direct,t.date_created,t.date_closed,t.notes,t.results 
     FROM cp_user_trades AS t 
     WHERE t.user_login = "user" 
     AND t.strat_id NOT IN (SELECT t.strat_id 
      FROM cp_user_trades AS t 
      INNER JOIN candles2 AS c 
      ON t.candle_id = c.id 
      WHERE t.user_login = "user" 
      AND t.active=0 
      AND t.deleted=0 
      ORDER BY t.date_closed DESC) 
     AND t.active=0 
     AND t.deleted=0 
     ORDER BY t.date_closed DESC 

我以爲t.strat_id是一個排除。我不知道主要(唯一)密鑰是什麼,但是行AND t.strat_id NOT IN (SELECT t.strat_id您可以用t.candle_id替換,如果這是重要的。還有其他類型的連接,但肯定比這更有效。