120 lines
7.9 KiB
HTML
120 lines
7.9 KiB
HTML
|
<?xml version="1.0" encoding="UTF-8"?>
|
||
|
<!DOCTYPE html
|
||
|
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||
|
<html lang="en-us" xml:lang="en-us">
|
||
|
<head>
|
||
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||
|
<meta name="security" content="public" />
|
||
|
<meta name="Robots" content="index,follow" />
|
||
|
<meta http-equiv="PICS-Label" content='(PICS-1.1 "http://www.icra.org/ratingsv02.html" l gen true r (cz 1 lz 1 nz 1 oz 1 vz 1) "http://www.rsac.org/ratingsv01.html" l gen true r (n 0 s 0 v 0 l 0) "http://www.classify.org/safesurf/" l gen true r (SS~~000 1))' />
|
||
|
<meta name="DC.Type" content="reference" />
|
||
|
<meta name="DC.Title" content="Multiple search conditions within a WHERE clause" />
|
||
|
<meta name="abstract" content="You can qualify your request further by coding a search condition that includes several predicates." />
|
||
|
<meta name="description" content="You can qualify your request further by coding a search condition that includes several predicates." />
|
||
|
<meta name="DC.subject" content="SELECT statement, WHERE, multiple search conditions, statements, SELECT, WHERE, multiple search conditions, examples, multiple search condition (WHERE clause), WHERE clause, multiple search condition within a, clause, multiple search condition within, WHERE clause, AND, AND keyword, AND keyword, multiple search condition, keyword, OR, OR keyword, OR keyword, multiple search condition, NOT, NOT keyword, example" />
|
||
|
<meta name="keywords" content="SELECT statement, WHERE, multiple search conditions, statements, SELECT, WHERE, multiple search conditions, examples, multiple search condition (WHERE clause), WHERE clause, multiple search condition within a, clause, multiple search condition within, WHERE clause, AND, AND keyword, AND keyword, multiple search condition, keyword, OR, OR keyword, OR keyword, multiple search condition, NOT, NOT keyword, example" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="rbafycomsrch.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="rbafycomsrch.htm" />
|
||
|
<meta name="DC.Relation" scheme="URI" content="rbafywhere.htm" />
|
||
|
<meta name="copyright" content="(C) Copyright IBM Corporation 1998, 2006" />
|
||
|
<meta name="DC.Rights.Owner" content="(C) Copyright IBM Corporation 1998, 2006" />
|
||
|
<meta name="DC.Format" content="XHTML" />
|
||
|
<meta name="DC.Identifier" content="rbafymultiplewhere" />
|
||
|
<meta name="DC.Language" content="en-us" />
|
||
|
<!-- All rights reserved. Licensed Materials Property of IBM -->
|
||
|
<!-- US Government Users Restricted Rights -->
|
||
|
<!-- Use, duplication or disclosure restricted by -->
|
||
|
<!-- GSA ADP Schedule Contract with IBM Corp. -->
|
||
|
<link rel="stylesheet" type="text/css" href="./ibmdita.css" />
|
||
|
<link rel="stylesheet" type="text/css" href="./ic.css" />
|
||
|
<title>Multiple search conditions within a WHERE clause</title>
|
||
|
</head>
|
||
|
<body id="rbafymultiplewhere"><a name="rbafymultiplewhere"><!-- --></a>
|
||
|
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
|
||
|
<h1 class="topictitle1">Multiple search conditions within a WHERE clause</h1>
|
||
|
<div><p>You can qualify your request further by coding a search condition
|
||
|
that includes several predicates.</p>
|
||
|
<div class="section"><p>The search condition you specify can contain any of the comparison
|
||
|
operators or the predicates BETWEEN, DISTINCT, IN, LIKE, EXISTS, IS NULL,
|
||
|
and IS NOT NULL.</p>
|
||
|
</div>
|
||
|
<div class="section"><p>You can combine any two predicates with AND and OR. In addition,
|
||
|
you can use the NOT keyword to specify that the search condition that you
|
||
|
want is the negated value of the specified search condition. A WHERE clause
|
||
|
can have as many predicates as you want.</p>
|
||
|
<ul><li><strong>AND</strong> says that, for a row to qualify, the row must satisfy both
|
||
|
predicates of the search condition. For example, to find out which employees
|
||
|
in department D21 were hired after December 31, 1987, specify: <pre>…
|
||
|
<strong>WHERE</strong> WORKDEPT = 'D21' <strong>AND</strong> HIREDATE > '1987-12-31'</pre>
|
||
|
</li>
|
||
|
<li><strong>OR</strong> says that, for a row to qualify, the row can satisfy the condition
|
||
|
set by either or both predicates of the search condition. For example, to
|
||
|
find out which employees are in either department C01 or D11, you can specify
|
||
|
: <pre>…
|
||
|
<strong>WHERE</strong> WORKDEPT = 'C01' <strong>OR</strong> WORKDEPT = 'D11'</pre>
|
||
|
<div class="note"><span class="notetitle">Note:</span> You
|
||
|
can also use IN to specify this request: WHERE WORKDEPT IN (<samp class="codeph">'</samp>C01<samp class="codeph">'</samp>, <samp class="codeph">'</samp>D11<samp class="codeph">'</samp>).</div>
|
||
|
</li>
|
||
|
<li><strong>NOT</strong> says that, to qualify, a row must not meet the criteria set
|
||
|
by the search condition or predicate that follows the NOT. For example, to
|
||
|
find all employees in department E11 except those with a job code equal to
|
||
|
analyst, you can specify: <pre>…
|
||
|
<strong>WHERE</strong> WORKDEPT = 'E11' <strong>AND NOT</strong> JOB = 'ANALYST'</pre>
|
||
|
</li>
|
||
|
</ul>
|
||
|
</div>
|
||
|
<div class="section"><p>When SQL evaluates search conditions that contain these connectors,
|
||
|
it does so in a specific order. SQL first evaluates the NOT clauses, next
|
||
|
evaluates the AND clauses, and then the OR clauses.</p>
|
||
|
</div>
|
||
|
<div class="section"><p>You can change the order of evaluation by using parentheses. The
|
||
|
search conditions enclosed in parentheses are evaluated first. For example,
|
||
|
to select all employees in departments E11 and E21 who have education levels
|
||
|
greater than 12, you can specify: </p>
|
||
|
<pre>…
|
||
|
<strong>WHERE</strong> EDLEVEL > 12 <strong>AND</strong>
|
||
|
(WORKDEPT = 'E11' <strong>OR</strong> WORKDEPT = 'E21')</pre>
|
||
|
<p>The parentheses
|
||
|
determine the meaning of the search condition. In this example, you want all
|
||
|
rows that have a: </p>
|
||
|
<ul><li>WORKDEPT value of E11 or E21, and</li>
|
||
|
<li>EDLEVEL value greater than 12</li>
|
||
|
</ul>
|
||
|
</div>
|
||
|
<div class="section"><p>If you did not use parentheses: </p>
|
||
|
<pre>…
|
||
|
<strong>WHERE</strong> EDLEVEL > 12 <strong>AND</strong> WORKDEPT = 'E11'
|
||
|
<strong>OR</strong> WORKDEPT = 'E21'</pre>
|
||
|
</div>
|
||
|
<div class="section"><p>Your result is different. The selected rows are rows that have:
|
||
|
</p>
|
||
|
<ul><li>WORKDEPT = E11 and EDLEVEL > 12, or</li>
|
||
|
<li>WORKDEPT = E21, regardless of the EDLEVEL value</li>
|
||
|
</ul>
|
||
|
</div>
|
||
|
<div class="section"><img src="./delta.gif" alt="Start of change" /><p>If you are combining multiple equal comparisons,
|
||
|
you can write the predicate with the ANDs as shown in the following example:</p>
|
||
|
<pre>…
|
||
|
<strong>WHERE</strong> WORKDEPT = 'E11' <strong>AND</strong> EDLEVEL = 12 <strong>AND</strong> JOB = 'CLERK'</pre>
|
||
|
<p>You can also compare two lists, for example:</p>
|
||
|
<pre>…
|
||
|
<strong>WHERE</strong> (WORKDEPT, EDLEVEL, JOB) = ('E11', 12, 'CLERK')</pre>
|
||
|
<p>When
|
||
|
two lists are used, the first item in the first list is compared to the first
|
||
|
item in the second list, and so on through both lists. Thus, each list must
|
||
|
contain the same number of entries. Using lists is identical to writing the
|
||
|
query with AND. Lists can only be used with the equal and not equal comparison
|
||
|
operators.</p>
|
||
|
<img src="./deltaend.gif" alt="End of change" /></div>
|
||
|
</div>
|
||
|
<div>
|
||
|
<div class="familylinks">
|
||
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="rbafycomsrch.htm" title="In addition to the basic comparison predicates (=, >, <, and so on), a search condition can contain any of the predicates BETWEEN, IN, EXISTS, IS NULL, and LIKE.">Define complex search conditions</a></div>
|
||
|
</div>
|
||
|
<div class="relref"><strong>Related reference</strong><br />
|
||
|
<div><a href="rbafycomsrch.htm" title="In addition to the basic comparison predicates (=, >, <, and so on), a search condition can contain any of the predicates BETWEEN, IN, EXISTS, IS NULL, and LIKE.">Define complex search conditions</a></div>
|
||
|
<div><a href="rbafywhere.htm" title="The WHERE clause specifies a search condition that identifies the row or rows you want to retrieve, update, or delete.">Specify a search condition using the WHERE clause</a></div>
|
||
|
</div>
|
||
|
</div>
|
||
|
</body>
|
||
|
</html>
|