WHERE condition
Sometimes when you manipulate a database, you manipulate only conditionally restricted data, and you can add a WHERE clause to the SQL statement to specify the conditions for the operation of the data.
Grammar:
Copy Code code as follows:
SELECT column,... From Tb_name WHERE definition
The WHERE keyword is followed by a valid expression (definition) that represents the condition that the data record being manipulated must meet.
In addition to SELECT, the WHERE condition keyword can be used in any situation that the SQL syntax allows, such as update (update), delete (delete), and so on.
Example:
Copy Code code as follows:
SELECT * FROM user WHERE username = ' Jack '
This example specifies that the query condition is username equal to Jack's data.
Operator description in WHERE expression:
Parameter description:
operator |
Description |
= |
Equals |
!= |
Not equal to, some database systems also write <> |
> |
Greater than |
< |
Less than |
>= |
Greater than or equal to |
<= |
Less than or equal to |
BETWEEN ... And ... |
Within a range, example: WHERE age BETWEEN 30 |
Not BETWEEN ... And ... |
Not within a certain range |
In (item 1, item 2,...) |
Within the specified item, example: Where City in (' Beijing ', ' Shanghai ') |
Not in (item 1, item 2,...) |
is not in the specified item |
Like |
Search matches, often used in conjunction with pattern matching characters |
Not like |
The anti-righteousness of like |
Is NULL |
Null-value-judging character |
is not NULL |
Non-null Judge character |
Not, and, or |
Logical operators that represent no, and, or, for multiple logical connections. Priority: not > and > OR |
% |
pattern-matching character, representing any string, example: WHERE username like '%user ' |
Some WHERE examples
To specify a user based on user name query:
Copy Code code as follows:
SELECT * FROM user WHERE username = ' Jack '
Check the name and ID number of the user registered after 0 o'clock in the morning January 1, 2009:
Copy Code code as follows:
$regdate = Mktime (00, 00, 01, 01, 01, 2009);
SELECT uid,username from user WHERE regdate >= $regdate
Search for all users in the username with the word User:
Copy Code code as follows:
SELECT * FROM user WHERE username like '%user% '
Search for all users in the username that contain user or admin:
Copy Code code as follows:
SELECT * FROM user WHERE username like '%user% ' OR username like '%admin% '