为什么这个mysql查询只返回null?

时间:2011-12-06 01:25:56

标签: mysql

我只是试图查询一个表来返回一个布尔值,它似乎只返回null。

$sTable4由列userid&列组成。 artistid

$artistid是传入脚本的参数(肯定是正确传递的)

$userid是当前登录用户的ID。

用户可以将艺术家保存到他们的favourites列表中。

我想要的是检查此表中当前登录的用户以及在上面的变量中声明的特定artistid,以查看是否存在条目。如果没有,那么我希望它返回null并返回'1',如果它。

我出错的任何想法?

SELECT userid, artistid
FROM $sTable4 AS b
WHERE NOT EXISTS
(
   SELECT artistid
   FROM $sTable4 AS ab
   WHERE b.userid = $userid
      AND b.artistid = $artistid
)

1 个答案:

答案 0 :(得分:1)

没有错,实际上你试图从同一个表中不存在的userid, artistid中选择$sTable4,那么你期望什么?如果它们不存在你会如何选择它们? ?!!我认为你在查询中遗漏了一些东西,因为这个检查总是会返回null,因为它们存在,所以它不会选择它们。所以我认为where exists会对你有用:

SELECT ab.artistid
FROM $sTable4 AS ab
WHERE exists 
(
    select artistid 
    from $sTable4 b 
    where b.userid=$userid AND b.artistid=$artistid
)
相关问题