SQL WHERE, AND, & OR > 기술자료 | 해피정닷컴

SQL WHERE, AND, & OR > 기술자료

본문 바로가기

사이트 내 전체검색

SQL WHERE, AND, & OR > 기술자료

ClassicASP SQL WHERE, AND, & OR

페이지 정보


본문

Often times a single search condition with a singe WHERE clause is not enough and we need to search on multiple conditions.
This is where additional logical conditions such as AND & OR come into play. Taking our previous example just a bit further we can see how this all works.
Plus, at the end of this post there is a warning on combining the AND & OR condition together.

Here is the data for our example again.

Table 1.
DOG_ORIGIN table

Country Breed Size
------- --------------------- -----
Germany German Shepherd Dog Big
Germany Dobermann Big
Germany Rottweiler Big
USA Siberian Husky Medium
USA Alaskan Malamute Medium
USA American Bulldog Big
Switzerland Bernese Mountain Dog Big
Switzerland Saint Bernard Dog Big
Switzerland Entlebuch Cattle Dog Medium
Australia Australian Cattle Dog Medium
Australia Jack Russell Terrier Small


This first example shows the first logical condition (AND) that most practitioners understand and typically use.

A logical condition simply combines the results of two component conditions to produce a single result.

The logical AND condition facilitates this purpose and is considered.
Just as you would suspect, when using the logical AND condition requires both conditions between the AND to be true for a result set to be returned.
If one condition is FALSE then the total condition is FALSE.
So if we wanted to look for medium sized dogs in the USA we would construct a query like the following:



SQL  > SELECT country, breed, breed_size
         FROM dog_origin
        WHERE country = 'USA'
          AND breed_size = 'Medium';

COUNTRY      BREED                BREED_SIZE
------------ -------------------- ----------
USA          Siberian Husky       Medium
USA          Alaskan Malamute     Medium




Another logical conditional operator is the OR condition.
The logical OR condition specifies that one or the other conditions need only be true for a result set to be returned.
If both conditions are False then the statement evaluates to False.

This is just as you would suspect and if we wanted to display the dogs from countries that were big or small we could construct the following:



SQL  > SELECT country, breed, breed_size
         FROM dog_origin
        WHERE breed_size = 'Big'
           OR breed_size = 'Small';

COUNTRY      BREED                BREED_SIZE
------------ -------------------  ----------
Germany      German Shepherd Dog  Big
Germany      Dobermann            Big
Germany      Rottweiler           Big
USA          American Bulldog     Big
Switzerland  Bernese Mountain Dog Big
Switzerland  Saint Bernard Dog    Big
Australia    Jack Russell Terrier Small




Problems arise when constructing a SQL statement that contains both logical operators AND and OR.
Great care must be given in regard to the order they are evaluated.
Just remember that the logical AND condition has a higher priority than the OR operator and thus gets evaluated first.

This is best explained by looking at an example.

Let's say we wanted to look for dogs in the USA that were either Big or Small. We might come up with the following SQL at first but recognize that the result set
is totally WRONG. Somehow we returned "Small" dogs from Australia.



SQL  > SELECT country, breed, breed_size
         FROM dog_origin
        WHERE country = 'USA' AND breed_size = 'Big' OR breed_size = 'Small';

COUNTRY                        BREED                          BREED_SIZE
------------------------------ ------------------------------ ----------
USA                            American Bulldog               Big
Australia                      Jack Russell Terrier           Small
2 rows selected.




We might re-arrange the SQL a little such as this next example but we quickly realize that it too has returned rows where the dogs are from different countries than that of the USA.


SQL  > SELECT country, breed, breed_size
         FROM dog_origin
        WHERE breed_size = 'Big' OR breed_size = 'Small' AND country = 'USA';

COUNTRY                        BREED                          BREED_SIZE
------------------------------ ------------------------------ ----------
Germany                        German Shepherd Dog            Big
Germany                        Dobermann                      Big
Germany                        Rottweiler                     Big
USA                            American Bulldog               Big
Switzerland                    Bernese Mountain Dog           Big
Switzerland                    Saint Bernard Dog              Big
6 rows selected.




So what can we do? The idea here is to realize that the logical conditions all have a presedence in the order they are evaluated. Just so happens that the AND
condition evaluates before the OR condition. So it is impossible to get the results we want without doing some better grouping of logical conditions. Here are two
different methods that get the proper result sets.


SQL  > SELECT country, breed, breed_size FROM dog_origin
        WHERE country = 'USA' AND breed_size = 'Big'
           OR country = 'USA' AND breed_size = 'Small';

COUNTRY                        BREED                          BREED_SIZE
------------------------------ ------------------------------ ----------
USA                            American Bulldog               Big

SQL  > SELECT country, breed, breed_size FROM dog_origin
        WHERE country = 'USA'
          AND ( breed_size = 'Big' OR breed_size = 'Small' );

COUNTRY                        BREED                          BREED_SIZE
------------------------------ ------------------------------ ----------
USA                            American Bulldog               Big



자료출처 : http://blogs.ittoolbox.com/database/solutions/archives/finding-data-sql-where-and-or-11191

댓글목록

등록된 댓글이 없습니다.


Total 2,641건 111 페이지
  • RSS
기술자료 목록
열람
ClassicASP   12826  2008-03-21 08:36  
440
일반   13382  2008-03-21 08:34 ~ 2018-07-10 21:24  
439
MSSQL   31578  2008-03-18 12:20  
438
WindowsServer   13581  2008-03-18 12:18  
437
ClassicASP   30325  2008-03-14 14:53 ~ 2018-03-24 04:08  
436
ClassicASP   13893  2008-03-13 08:04  
435
WindowsServer   13770  2008-03-13 21:47  
434
ClassicASP   12007  2008-03-13 07:31  
433
일반   15894  2008-03-11 16:37  
432
ClassicASP   30440  2008-03-09 18:44 ~ 2018-05-15 14:37  
431
ClassicASP   23257  2008-03-08 10:40 ~ 2013-11-28 00:00  
430
ClassicASP   25008  2008-03-08 10:34 ~ 2014-07-08 00:00  
429
그누보드   13250  2008-03-07 17:38 ~ 2008-03-17 00:00  
428
그누보드   16140  2008-03-05 21:37 ~ 2021-07-14 18:37  
427
그누보드   12289  2008-02-14 20:33  
426
그누보드   13610  2008-02-14 14:45  
425
Adobe   19862  2008-02-11 21:16  
424
그누보드   17870  2008-02-11 21:25 ~ 2008-02-20 00:00  
423
그누보드   11408  2008-02-05 20:12  
422
테크노트   14673  2008-02-02 12:15  

검색

해피정닷컴 정보

회사소개 회사연혁 협력사 오시는길 서비스 이용약관 개인정보 처리방침

회사명: 해피정닷컴   대표: 정창용   전화: 070-7600-3500   팩스: 042-670-8272
주소: (34368) 대전시 대덕구 대화로 160 대전산업용재유통단지 1동 222호
개인정보보호책임자: 정창용   사업자번호: 119-05-36414
통신판매업신고: 제2024-대전대덕-0405호 [사업자등록확인]  
Copyright 2001~2024 해피정닷컴. All Rights Reserved.