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
,       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 に答える
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はデータベースに存在しませんが、クエリは、DBにないInfo-Userを含むコードからとにかく彼を表示する必要があります。500人のユーザーの場合、全員を個別に確認することはできません:/

于 2013-02-28T07:27:14.550 に答える