子查询的问题

时间:2012-03-12 04:35:45

标签: mysql sql

查询:

SELECT id, username,
       (SELECT username 
          FROM users 
         WHERE email = '{$email}' 
           AND activate = 0 ) AS inactive
  FROM users
 WHERE email = '{$email}'
   AND password = '{$password}'
   AND activate = 1

字段电子邮件为UNIQUE,因此电子邮件中没有重复。

如果组合username存在,我想输出idemail,password,activate=1如果组合username,我想输出inactive email,activate=0存在。

更新:这是有效的

       SELECT id,

            (SELECT username 
               FROM users 
              WHERE email = '{$email}' 
            AND activate = 0
            ) AS inactive,

            (SELECT username 
               FROM users 
              WHERE email = '{$email}' 
            AND activate = 1
            AND password = '{$password}'
            ) AS active

            FROM users
            WHERE email = '{$email}'

1 个答案:

答案 0 :(得分:1)

尝试以下

select id, 
   case activate
      when 0 then 'inactive'
      when 1 then username
   end as username
from users
WHERE email = '{$email}'
   AND (activate = 0 or password = '{$password}')

case语句用于在列或表达式的多个选项之间进行选择