2013-02-25 1 views
2

I'v 존재하지 않는 행에 의해 정보를 반환 SQL은는 SQLPLUS에서이 문제 가지고

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

하지만이 필요합니다 :

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

를 내가 얻을

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

또는 이와 비슷한 내용입니다. 사용자가 존재하지 않는 경우,

이 제발 도와주세요,

감사합니다,

답변

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에. 500 명의 사용자의 경우 모두를 확인할 수는 없습니다./

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 
+0

사용자 목록이 동적이지 않은 경우에만 작동합니다. 질문 저자는 여기에 어떤 경우인지 명확하게하지 않습니다. –

+0

흠, 불행히도 ISNULL() 함수가 작동하지 않습니다 .. SqlServer 함수가 작동하지 않습니다./ – Dave

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 
0
SELECT DISTINCT username, 
ISNULL(Name,'Not Exists'), 
ISNULL(Surname,'Not Exists') 
FROM users 
    WHERE (username IN ('user1','user2') OR username IS NULL) 
+0

안녕하세요, 모두에게 감사드립니다. – Dave

+0

코드 : select 별명 username, isnull (name, 'nothing') ISNULL (m 고용주로부터 성 '아무것도') , m.employers_nr = w.employers_nr 및 ( 'USER1'에 (사용자 이름, 년대 USER2 ' w 고용주) OR 자명)는 널 (NULL); 오류가 발생합니다. ORA-00904 : "ISNULL": 잘못된 식별자 – Dave

관련 문제