2012-02-16 60 views
1

我想選擇使用內連接到另一個表的行。有COUNT()= 0的困惑

SELECT COUNT(t3.ID), t3.act, t3.dj_id, t1.ID, 
    t1.name, t1.label_id, t1.date, t1.genre, 
    t1.plink, t1.featana, t1.promo_aktif 
FROM `release` t1 
INNER JOIN `acts` t3 ON t3.release_id = t1.ID 
WHERE t1.ID IN ($lst) AND t1.promo_aktif = 'Active' 
AND t3.act = 'hide' AND t3.dj_id = '$id' 
HAVING COUNT(t3.ID) = 0 
ORDER BY t1.ID DESC 
LIMIT 0,15 

在此查詢中,結果爲空。它選擇0行。但我確信有這種條件的行。

查詢需要選擇t1行的時候沒有在t3

我在哪裏錯在此查詢找到的記錄?

感謝

編輯

正確的代碼是

SELECT t3.act, t3.dj_id, t1.ID, 
    t1.name, t1.label_id, t1.date, t1.genre, 
    t1.plink, t1.featana, t1.promo_aktif 
FROM `release` t1 
LEFT JOIN `acts` t3 ON t3.release_id = t1.ID AND t3.act = 'hide' AND t3.dj_id = '$id' 
WHERE t1.ID IN ($lst) AND t1.promo_aktif = 'Active' 
AND t3.release_id is null 
ORDER BY t1.ID DESC 
LIMIT 0,15 
+6

'查詢需要選擇T1行的時候沒有在t3'找到的記錄---不是不是什麼' INNER JOIN'應該這樣做。 – zerkms 2012-02-16 01:13:31

+0

如何在t3不返回行時選擇t3.act? – 2012-02-16 01:17:25

+3

幫助你一個忙,並學習在逗號後面使用空格鍵。它可以更容易地告訴你正在使用哪個列(當然,我指的是使用't1.name,t1.label_id,t1.date'而不是't1.name,t1.label_id,t1。日期「。第一種情況下,當您需要稍後返回添加或刪除列時,它的可讀性要高得多,並且使其他人必須閱讀它更容易*) – 2012-02-16 01:19:49

回答

0

正確的代碼是

SELECT t3.act, t3.dj_id, t1.ID, 
    t1.name, t1.label_id, t1.date, t1.genre, 
    t1.plink, t1.featana, t1.promo_aktif 
FROM `release` t1 
LEFT JOIN `acts` t3 ON t3.release_id = t1.ID AND t3.act = 'hide' AND t3.dj_id = '$id' 
WHERE t1.ID IN ($lst) AND t1.promo_aktif = 'Active' 
AND t3.release_id is null 
ORDER BY t1.ID DESC 
LIMIT 0,15 
0

試試這個:

SELECT t3.act, t3.dj_id, t1.ID, 
    t1.name, t1.label_id, t1.date, t1.genre, 
    t1.plink, t1.featana, t1.promo_aktif 
FROM `release` t1 
LEFT JOIN `acts` t3 ON t3.release_id = t1.ID 
WHERE t1.ID IN ($lst) AND t1.promo_aktif = 'Active' 
    AND t3.act = 'hide' AND t3.dj_id = '$id' 
      AND t3.release_id is null 
ORDER BY t1.ID DESC 
LIMIT 0,15 

不知道你的代碼,我不相信這個條件:t3.dj_id = '$id'

+0

不起作用。它還顯示在t3 – 2012-02-16 01:23:46

+0

中找到的行仍然不起作用。我看到空的結果。 – 2012-02-16 01:28:22

+0

然後它只是你的過濾器的問題。在你的代碼中必須出現錯誤:'t1.ID IN($ lst)AND t1.promo_aktif ='Active'AND t3.act ='hide'AND t3.dj_id ='$ id''。通過「錯誤」我的意思是它過濾比它應該更多。刪除所有這些過濾器並逐步添加它們以查找。特別注意't3.dj_id ='$ id'' – 2012-02-16 01:39:37

0

這應該給你想要的結果:

SELECT t1.ID, 
    t1.name, t1.label_id, t1.date, t1.genre, 
    t1.plink, t1.featana, t1.promo_aktif 
FROM `release` t1 
LEFT JOIN `acts` t3 ON t3.release_id = t1.ID 
WHERE t1.ID IN ($lst) AND t1.promo_aktif = 'Active' 
AND t3.release_id IS NULL 
ORDER BY t1.ID DESC 
LIMIT 0,15