2

我在 SQLPlus 上遇到了这个问题:

从这段代码:

select distinct username, name, surname
from users
where username in ('user1', 'user2');

我得到:

username         name           surname   
--------------- -------------- -------------
user1            Alex            Ander

但是我需要:

username         name           surname   
--------------- -------------- -------------
user1            Alex            Ander
user2            not exists     not exists

或者类似的东西。如果用户不存在,表必须写用户名,其余的也像“不存在”,

请帮忙,

谢谢,

4

4 回答 4

1

这也应该有效;

select distinct T.username, 
                coalesce(u.name, 'not exists'), 
                coalesce(u.surname, 'not exists')
from  (values ('user1'),('user2'),('user3')) as T(username)
      left join Users u on T.username = u.username
于 2013-02-25T12:36:18.580 回答
0

希望现在更清楚地解释它:

我现在拥有的原始代码是:

select distinct username, name, surname
from users u, accounts a
where u.user_nr = a.user_nr
and username in (
'existing_user',
'not_existing_user'
) order by username;

它给了我:

USERNAME                  NAME            SURNAME  
------------------------- --------------- ---------------
existing_user              Hello           All

1 row selected.

我需要:

USERNAME                  NAME            SURNAME  
------------------------- --------------- ---------------
existing_user             Hello           All
not_existing_user     Not Exists      Not Exists

2 row selected.

问题:用户 not_existing_user 在数据库中不存在,但查询必须从代码中显示他,信息 - 用户不在数据库中。对于 500 个用户,我不能单独检查每个人:/

于 2013-02-28T07:27:14.550 回答
0
select  distinct username
,       coalesce(name, 'not exists')
,       coalesce(surname, 'not exists')
from    (
        select 'user1' as username
        union all
        select 'user2'
        ) list
left join
        users
on      list.username = users.username
于 2013-02-25T12:25:43.080 回答
0
SELECT DISTINCT username,
ISNULL(Name,'Not Exists'),
ISNULL(Surname,'Not Exists')
FROM users
  WHERE (username IN ('user1','user2') OR username IS NULL)
于 2013-02-25T12:37:31.677 回答