select id, username, email,
case
when username = email then 'true'
else 'false'
end
from users
有没有简单的方法when username = email then 'true' as order_col
或from users where case = true
问题是您能否将案例包装起来AS case
,然后对其进行排序或在该案例上使用 Where 语句。
select id, username, email,
case when username = email
then 'true'
else 'false'
end as usr_is_email
from users
order by usr_is_email
您可以在 中使用别名,ORDER BY
但不能在 中使用WHERE
,因此可以:
SELECT id, username, email,
CASE
WHEN username = email THEN 'true'
ELSE 'false'
end AS UserNameIsEmail
FROM users
ORDER BY UserNameIsEmail DESC,
username ASC
如果您希望它在WHERE
重复它或使用子查询中使用:
SELECT id, username, email, usernameisemail
FROM (SELECT id,
username,
email,
CASE
WHEN username = email THEN 'true'
ELSE 'false'
end AS UserNameIsEmail
FROM users)X
WHERE X.usernameisemail = 'true'
ORDER BY username ASC