对COUNT()= 0感到困惑

时间:2012-02-16 01:11:43

标签: mysql database

我想选择使用内连接到另一个表的行。

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

3 个答案:

答案 0 :(得分: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'

答案 1 :(得分: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

答案 2 :(得分: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