MySQL Where

To perform conditional restrictions when retrieving data, then you can add a WHERE clause in the SQL statement to specify the conditions for data manipulation.


SELECT column, ... FROM tb_name WHERE definition

The WHERE keyword is followed by a valid expression that represents the conditions that must be met for the data record being manipulated.

In addition to SELECT, WHERE condition keywords can be used in any SQL syntax allowed, such as UPDATE (update), DELETE (delete).


SELECT * FROM user WHERE username = 'Jack'

This example specifies that the query condition is username equal to Jack's data.

WHERE operators

operator Description
= equals
!= is not equal, and some database systems are also written as < >
> greater than
< less than
>= greater than or equal to
<= less than or equal to
BETWEEN ... AND ... is somewhere in the range, for example: WHERE age BETWEEN 20 AND 30
NOT BETWEEN ... AND ... is not within a certain range
IN(item1, item2, ...) In the specified item, for example: WHERE city IN('New York', 'Los Angeles')
NOT IN(item 1, item 2, ...) is not in the specified
LIKE Search for matches, often used in conjunction with pattern matching
NOT LIKE LIKE antisense
IS NULL Null value judgment
IS NOT NULL non-null judgment
NOT, AND, OR Logical operators, denoting no, and, or, respectively, for multiple logical connections.
Priority: NOT > AND > OR
% pattern matching, that any string, for example: WHERE username LIKE '% user'

Other examples

Query the specified user by username:

SELECT * FROM user WHERE username = 'Jack'

After specific date

SELECT uid, username FROM user WHERE regdate >= '2014-12-31

Search for all users with name contains 'user' in the username:

SELECT * FROM user WHERE username LIKE '%user%'

Search for all users with name contains 'user' or 'admin' in the username:

SELECT * FROM user WHERE username LIKE '%user%' OR username LIKE '%admin%'

Copyright 2014-2017 by All Rights Reserved.