2017-09-28 25 views
0

我目前正在執行此查詢,但是,我總是得到一個結果,應該沒有,因爲有一個相應的行。SQL查詢加入並驗證是否沒有對應的行

select `likes`.*, `likeshistory`.`like_id`, `likeshistory`.`instagram_id` from `likes` join `likeshistory` on `likes`.`id` = `likeshistory`.`like_id` where (`finished` = '0' and `active` = '1' and `banned` = '0') and `instagram_id` not in ('2') limit 1 

基本上我想選擇一個 「喜歡」 有沒有 「likeshistory」 爲(instagram_id = 2)

likeshistory structure

謝謝! William

+0

請問您可以發佈樣本數據和結構嗎? –

回答

0

您可以使用not exists。該查詢的目的是更清楚:

select `likes`.*, `likeshistory`.`like_id`, `likeshistory`.`instagram_id` 
from `likes` l 
where not exists (select 1 
        from `likeshistory` lh 
        where l.id = lh.`like_id` and 
         (`finished` = '0' and `active` = '1' and `banned` = '0') and lh.instagram_id not in ('2') 
       ); 

如果任何在子查詢(除了相關子句)的條件是上likes,那麼那些應在外部查詢。