database.sarang.net
UserID
Passwd
Database
DBMS
MySQL
PostgreSQL
Firebird
Oracle
Informix
ㆍSybase
MS-SQL
DB2
Cache
CUBRID
LDAP
ALTIBASE
Tibero
DB 문서들
스터디
Community
공지사항
자유게시판
구인|구직
DSN 갤러리
도움주신분들
Admin
운영게시판
최근게시물
Sybase Q&A 1926 게시물 읽기
No. 1926
필드에 값이 있는지 체크하는 방법..
작성자
경아
작성일
2007-05-25 14:12
조회수
5,243



테이블에 필드가 1, 2, 3, 4 가 있다고 가정하고,  그 넷중에 하나라도 값이 있으면 보여주고 
네개의 필드가 모두 null 이라면 안보여주게 하고 싶은데,  방법을 모르겠어요.

덧붙여, having 의 사용법도 좀 더 자세히 알고 싶습니다. 도와주세용!!

이 글에 대한 댓글이 총 4건 있습니다.

1> or 조건으로 묶어서 사용하면 됩니다.

select * from test

where

a is not null 

or  b is not null

or c is not null 

or d is not null


2>having 은 group by한 결과를 다시 제한한때사용됩니다

create table #temp

(

  c char(1),

  d char(2)

)

insert #temp values ('1', 'a')

insert #temp values ('1', 'b')

insert #temp values ('2', 'c')


-- c 컬럼으로 묶었을때 건수가 1개인것을 찾기위해


select c, count(1) as cnt into #temp2 from #temp group by c

select * from #temp2

c    cnt        

-    -----------

1              2

2              1 

select c  from #temp2 where cnt = 1

c   

-   

2

위 쿼리를

select c from #temp group by c having count(1) = 1

having 절을 사용하여 1개의 쿼리로 바꿀수 있습니다.

영빈~(backfish)님이 2007-05-25 14:34에 작성한 댓글입니다.
이 댓글은 2007-05-25 14:39에 마지막으로 수정되었습니다.

글쿤요... or로 수정 했습니다.

영빈~(backfish)님이 2007-05-25 14:40에 작성한 댓글입니다.

어떻게 된건지 and 로 해도 값이 똑같이 나옵니다. 신기하네요 ^^;

최정경님이 2007-05-25 15:05에 작성한 댓글입니다. Edit

create table #temp
(
  pk char(1) not null,
  a char(1) null,
  b char(2) null,
  c char(2) null,
  d char(2) null
)

insert #temp values ('1', null,null,null,null)
insert #temp values ('2', '1',null,null,null)
insert #temp values ('3', '1','2','3','4')
insert #temp values ('4', '',null,null,null)

1>
 select * from #temp
where
a is not null
or b is not null
or c is not null
or d is not null
결과
pk   a    b    c    d   
--   -    -    -    -   
2    1    NULL NULL NULL
3    1    2    3    4   
4         NULL NULL NULL

2>
select * from #temp
where
a is not null
and b is not null
and c is not null
and d is not null
결과
pk   a    b    c    d   
--   -    -    -    -   
3    1    2    3    4  

3>
select * from #temp
where
rtrim(a) is not null
or rtrim(b) is not null
or rtrim(c) is not null
or rtrim(d) is not null
결과
pk   a    b    c    d   
--   -    -    -    -   
2    1    NULL NULL NULL
3    1    2    3    4    

각 컬럼에 null이 아니라 space가 들어가 있는 듯 합니다.
3번처럼 rtrim해서 사용하면 될 듯..

영빈~(backfish)님이 2007-05-25 15:17에 작성한 댓글입니다.
[Top]
No.
제목
작성자
작성일
조회
1929union all 하려는데 질문이 있어요^^ [2]
경아
2007-05-28
5517
1928alter database로 디바이스 추가시 [4]
궁금이
2007-05-25
5527
1927오라클의 external table과 같은 기능이 있나??? [2]
김종석
2007-05-25
5951
1926필드에 값이 있는지 체크하는 방법.. [4]
경아
2007-05-25
5243
1925뷰에 관한 질문... [2]
경호선
2007-05-24
6234
1924오라클의 greatest와 대응되는 함수나 SQL좀 [2]
초옵
2007-05-23
6082
1923[긴급] 커리좀 봐주세요... [3]
김철겸
2007-05-23
5655
Valid XHTML 1.0!
All about the DATABASE... Copyleft 1999-2024 DSN, All rights reserved.
작업시간: 0.020초, 이곳 서비스는
	PostgreSQL v16.2로 자료를 관리합니다