MySQL CASE语句是否优先?

时间:2014-02-11 01:43:41

标签: mysql sql

我正在努力查询有一些空值的查询,而在我的CASE语句中,没有处理NULL值的测试。我试过以不同的方式订购测试,但它似乎没有什么区别。

SELECT Title, library.LibraryID, Registered
FROM libraries
LEFT JOIN 
(SELECT LibraryID,UserID,
        (CASE  
>>>                WHEN UserID IS NULL THEN "False"     <<<<<<<< PROBLEM
               WHEN UserID=1 THEN "YES! Registered" 
                   ELSE "Not Registered" 
     END) AS Registered
FROM librariesandmembers WHERE UserID=1) AS myAlias 
ON libraries.LibraryID = myAlias.LibraryID');

然后我尝试了

WHEN USerID<>1 THEN "False"

但这并没有改变任何事情。

这是一个PHP / MySQL应用程序。 phpmyAdmin中的结果显示我的查询结果正在生成NULLS,我想要“False”出现。

我错过了什么?必须是明显的东西,但我只是看不到它。

[编辑]让我尝试用这种方式解释目标:我希望列出所有(例如5个)库,其旁边的结果是“是!已注册”或“未注册”。在librariesandmembers表中可能有也可能没有相应的条目。因此是NULL问题。

4 个答案:

答案 0 :(得分:1)

我几乎可以肯定这与您的加入而不是您的案例陈述有关。也就是说,使用开关来构建子查询结果集以处理NULL,然后将LOJ构建到库中,有效地创建完全空的记录,其中不存在匹配。因此,您对“已注册”专栏的努力是徒劳的。尝试重新构建您的查询,如下所示:

SELECT 
  Title, 
  libraries.LibraryID, 
  (
    CASE
      WHEN librariesandmembers.UserID IS NOT NULL THEN "YES! Registered"
      ELSE "Not Registered"
    END
  ) as Registered
FROM 
  libraries LEFT JOIN librariesandmembers
    ON libraries.LibraryID = librariesandmembers.LibraryID
    AND librariesandmembers.UserID = 1      

此外,您正在处理UserID = 1标准两次。您的子查询有效地删除了所有其他UserID,因此您不需要ELSE子句(当UserID既不是1也不是NULL时处理它)。我想你可以将我建议的WHERE子句移动到第二个JOIN条件,但我认为这会得到相同的结果。也许有人可以争辩/反对我的做法?

答案 1 :(得分:1)

这是您的查询:

SELECT Title, l.LibraryID, Registered
FROM libraries lLEFT JOIN 
      (SELECT LibraryID, UserID,
              (CASE WHEN UserID IS NULL THEN "False"     <<<<<<<< PROBLEM
                    WHEN UserID=1 THEN "YES! Registered" 
                   ELSE "Not Registered" 
               END) AS Registered
       FROM librariesandmembers
       WHERE UserID = 1
      ) AS myAlias 
      ON l.LibraryID = myAlias.LibraryID;

在子查询中,userId的值始终为1,因此case语句仅显示'YES! Registered'。你想把它移到外层。要获得相同的效果,条件UserId=1必须移至on子句:

SELECT Title, l.LibraryID, Registered,
       (CASE WHEN myalias.UserID IS NULL THEN 'False'
             WHEN myalias.UserID = 1 THEN 'YES! Registered'
             ELSE 'Not Registered'
        END) AS Registered
FROM libraries l LEFT JOIN 
     librariesandmembers myalias
      ON l.LibraryID = myAlias.LibraryID and
         myalias.UserID = 1;

答案 2 :(得分:0)

试试这个

SELECT 
  Title, 
  libraries.LibraryID, 
  (
    CASE
      WHEN librariesandmembers.UserID IS Null OR librariesandmembers.UserID ='' THEN 'False'
      When librariesandmembers.UserID = 1 Then 'YES! Registered'
      ELSE "Not Registered"
    END
  ) as Registered
FROM 
  libraries LEFT JOIN librariesandmembers
    ON libraries.LibraryID = librariesandmembers.LibraryID
WHERE 
  librariesandmembers.UserID = 1

答案 3 :(得分:0)

怎么样

SELECT a.Title, a.LibraryID, IFNULL(myAlias.Registered,'Not Registered') Registered
FROM libraries a
     LEFT JOIN 
        (SELECT b.LibraryID, ('YES! Registered') Registered
         FROM librariesandmembers b
         WHERE b.UserID=1) AS myAlias  ON libraries.LibraryID = myAlias.LibraryID

或者这个

SELECT a.Title, 
       a.LibraryID, 
       IF((SELECT COUNT(*) 
           FROM librariesandmembers b
           WHERE b.LibraryID=a.LibraryID AND b.UserID=1)=0,'Not Registered','Yes! Registered') Registered
FROM libraries a

在子查询中,只返回了两个有效值。成员中不存在该用户时的UserID或NULL。这就是CASE声明不起作用的原因。