mysql比较列和子查询

时间:2012-06-08 13:29:20

标签: mysql comparison subquery

sql查询:

select
usr.username,
(
  select usr2.username
  from users as usr2
) as another_user
from users as usr
having usr.username != another_user

如何将usr.username与子查询进行比较,例如命名为another_user

我想获得usr.username不等于another_user

的所有结果

我尝试了WHERE子句或HAVING,但仍然出现错误

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax

2 个答案:

答案 0 :(得分:0)

尝试

select
usr.username,
(
  select username from users where ...
) as another_user
from users as usr
where usr.username <> another_user.username

答案 1 :(得分:0)

也许是这样的:

SELECT
    *
FROM
(
    select
    usr.username,
    (
      ....
    ) as another_user
    from users as usr
) AS T
WHERE
    NOT T.username = T.another_user