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 198건 9 페이지
  • RSS
기술자료 목록
38
ClassicASP   12250  2008-04-24 11:47 ~ 2012-03-24 00:00  
37
ClassicASP   14559  2008-04-23 22:02 ~ 2015-02-03 00:00  
36
ClassicASP   14353  2008-04-17 22:30 ~ 2013-06-19 00:00  
35
ClassicASP   18530  2008-04-16 22:54  
34
ClassicASP   19168  2008-04-04 04:45  
33
ClassicASP   12799  2008-03-29 16:40  
32
ClassicASP   26987  2008-03-24 17:55 ~ 2012-05-18 00:00  
열람
ClassicASP   12851  2008-03-21 08:36  
30
ClassicASP   30340  2008-03-14 14:53 ~ 2018-03-24 04:08  
29
ClassicASP   13918  2008-03-13 08:04  
28
ClassicASP   12019  2008-03-13 07:31  
27
ClassicASP   30469  2008-03-09 18:44 ~ 2018-05-15 14:37  
26
ClassicASP   23272  2008-03-08 10:40 ~ 2013-11-28 00:00  
25
ClassicASP   25015  2008-03-08 10:34 ~ 2014-07-08 00:00  
24
ClassicASP   10726  2008-01-10 08:48  
23
ClassicASP   27138  2007-12-29 11:18  
22
ClassicASP   10981  2007-12-28 14:42 ~ 2008-10-17 00:00  
21
ClassicASP   22843  2007-09-28 22:39 ~ 2009-11-11 00:00  
20
ClassicASP   11695  2007-09-28 21:47  
19
ClassicASP   17697  2007-09-22 18:30 ~ 2013-12-26 00:00  

검색

해피정닷컴 정보

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

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