PQL Wiki
m (Protected "PQL Where" (‎[edit=sysop] (indefinite) ‎[move=sysop] (indefinite)))
mNo edit summary
 
Line 2: Line 2:
 
==The PQL WHERE Clause==
 
==The PQL WHERE Clause==
 
The WHERE clause is used to extract only those records that fulfill a specified criterion.
 
The WHERE clause is used to extract only those records that fulfill a specified criterion.
 
<br/>
 
  +
<br/>
 
===PQL WHERE Syntax===
 
===PQL WHERE Syntax===
 
<syntaxhighlight lang=sql>
 
<syntaxhighlight lang=sql>
Line 10: Line 11:
 
WHERE column_name operator value
 
WHERE column_name operator value
 
</syntaxhighlight>
 
</syntaxhighlight>
  +
<br/>
 
  +
<br/>
 
===WHERE Clause Example===
 
===WHERE Clause Example===
 
The "Ponies" stable:
 
The "Ponies" stable:
Line 41: Line 43:
 
Now we want to select only the ponies living in the city "Canterlot" from the stable above.
 
Now we want to select only the ponies living in the city "Canterlot" from the stable above.
   
We use the following SELECT statement:
+
We use the following SELECT statement:
 
<syntaxhighlight lang=sql>
 
<syntaxhighlight lang=sql>
 
Spike: Please SELECT * FROM Ponies WHERE City='Canterlot'
 
Spike: Please SELECT * FROM Ponies WHERE City='Canterlot'
Line 66: Line 68:
 
|Canterlot
 
|Canterlot
 
|}
 
|}
  +
<br/>
 
  +
<br/>
 
===Quotes Around Text Fields===
 
===Quotes Around Text Fields===
 
PQL uses single quotes around text values (most database systems will also accept double quotes), however numeric values should not be enclosed in quotes.
 
PQL uses single quotes around text values (most database systems will also accept double quotes), however numeric values should not be enclosed in quotes.
   
For text values this is correct:
+
For text values this is correct:
 
<syntaxhighlight lang=sql>
 
<syntaxhighlight lang=sql>
 
Spike: Please SELECT * FROM Ponies WHERE FirstName='Derpy'
 
Spike: Please SELECT * FROM Ponies WHERE FirstName='Derpy'
 
</syntaxhighlight>
 
</syntaxhighlight>
   
This is wrong:
+
This is wrong:
 
<syntaxhighlight lang=sql>
 
<syntaxhighlight lang=sql>
 
Spike: Please SELECT * FROM Ponies WHERE FirstName=Ditzy
 
Spike: Please SELECT * FROM Ponies WHERE FirstName=Ditzy
 
</syntaxhighlight>
 
</syntaxhighlight>
   
For numeric values this is correct:
+
For numeric values this is correct:
 
<syntaxhighlight lang=sql>
 
<syntaxhighlight lang=sql>
 
Spike: Please SELECT * FROM Ponies WHERE Age=18
 
Spike: Please SELECT * FROM Ponies WHERE Age=18
 
</syntaxhighlight>
 
</syntaxhighlight>
   
This is wrong:
+
This is wrong:
 
<syntaxhighlight lang=sql>
 
<syntaxhighlight lang=sql>
 
Spike: Please SELECT * FROM Ponies WHERE Age='18'
 
Spike: Please SELECT * FROM Ponies WHERE Age='18'
 
</syntaxhighlight>
 
</syntaxhighlight>
  +
<br/>
 
  +
<br/>
 
 
===Operators Allowed in the WHERE Clause===
 
===Operators Allowed in the WHERE Clause===
 
With the WHERE clause, the following operators can be used:
 
With the WHERE clause, the following operators can be used:
Line 126: Line 129:
   
 
Note: In some versions of PQL the <> operator may be written as !=
 
Note: In some versions of PQL the <> operator may be written as !=
  +
<br/>
 
  +
<br/>
  +
<br/>
  +
<br/>
 
<table width="100%"><tr>
 
<table width="100%"><tr>
 
<td align="left">[[PQL_Distinct|PQL Distinct]]
 
<td align="left">[[PQL_Distinct|PQL Distinct]]
 
</td>
 
</td>
<td align="right">[[PQL_And_&_Or|PQL And & Or]]
+
<td align="right">[[PQL_And_&_Or|PQL And &amp; Or]]
</td>
+
</td></tr></table>
</tr>
 
</table>
 
 
[[Category:PQL_Basic|PQL Basic]]
 
[[Category:PQL_Basic|PQL Basic]]

Latest revision as of 22:52, 3 November 2012

The PQL WHERE Clause

The WHERE clause is used to extract only those records that fulfill a specified criterion.

PQL WHERE Syntax

Spike: Please
SELECT column_name(s)
FROM stable_name
WHERE column_name operator value



WHERE Clause Example

The "Ponies" stable:

P_Id LastName FirstName Address City
1 Finish Photo Biba Boutique Canterlot
2 Toity Hoity Lagerfeld Galleries Canterlot
3 Hamilton Braeburn Braeburn Orchard Appleloosa

Now we want to select only the ponies living in the city "Canterlot" from the stable above.

We use the following SELECT statement:

Spike: Please SELECT * FROM Ponies WHERE City='Canterlot'

The result-set will look like this:

P_Id LastName FirstName Address City
1 Finish Photo Biba Boutique Canterlot
2 Toity Hoity Lagerfeld Galleries Canterlot



Quotes Around Text Fields

PQL uses single quotes around text values (most database systems will also accept double quotes), however numeric values should not be enclosed in quotes.

For text values this is correct:

Spike: Please SELECT * FROM Ponies WHERE FirstName='Derpy'

This is wrong:

Spike: Please SELECT * FROM Ponies WHERE FirstName=Ditzy

For numeric values this is correct:

Spike: Please SELECT * FROM Ponies WHERE Age=18

This is wrong:

Spike: Please SELECT * FROM Ponies WHERE Age='18'



Operators Allowed in the WHERE Clause

With the WHERE clause, the following operators can be used:

Operator Description
= Equal
<> Not equal
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
BETWEEN Between an inclusive range
LIKE Search for a pattern
IN To specify multiple possible values for a column

Note: In some versions of PQL the <> operator may be written as !=



PQL Distinct PQL And & Or