Home > Backend Development > PHP Problem > where in php query statement

where in php query statement

WBOY
Release: 2023-05-06 19:22:06
Original
1379 people have browsed it

Use of statements

When performing database operations, query statements are a very common operation method. In the query statement, the where statement is a very important part. It can be used to filter out data that meets specific conditions, thereby improving query efficiency and accuracy. This article will introduce the use of where statements in PHP from the aspects of basic syntax and common operations.

1. Basic syntax

In PHP, the basic syntax of the query statement is as follows:

SELECT column1, column2, ... FROM table_name WHERE condition
Copy after login

Among them, column1, column2 represent the field names in the table that need to be queried, you can Writing * indicates querying all fields; table_name indicates the name of the table to be queried; condition is the keyword of the where statement, indicating the query condition. It can be one or more conditions, and multiple conditions can be connected using AND/OR. .

Conditions usually include some comparison operators, such as =, <, >, etc. In addition, you can also use some special operators, such as IN, BETWEEN, LIKE, etc., which can be used for more complex query conditions. The following are introduced one by one:

1. Comparison operators:

Comparison operators are used to compare the size between two values. Common operators are as follows:

=    等于
<    小于
>    大于
<=   小于等于
>=   大于等于
<>   不等于
Copy after login

For example, as follows The statement is used to query the information of students older than 18 years old in the students table:

SELECT * FROM students WHERE age > 18
Copy after login

2.IN operator:

IN operator is used to query whether the value of a certain field is in the given In a certain set of values, the syntax format is as follows:

SELECT * FROM table_name WHERE column_name IN (val1, val2, val3, ...)
Copy after login

For example, the following statement is used to query the information of students with ages 18, 19, and 20:

SELECT * FROM students WHERE age IN (18, 19, 20)
Copy after login

3.BETWEEN operator:

The BETWEEN operator is used to query whether the value of a certain field is within a given interval. The syntax format is as follows:

SELECT * FROM table_name WHERE column_name BETWEEN val1 AND val2
Copy after login

For example, the following statement is used to query whether the age is between 18 and Information about students between 20 years old:

SELECT * FROM students WHERE age BETWEEN 18 AND 20
Copy after login

4. LIKE operator:

The LIKE operator is used to query data through wildcard matching. The more common wildcard characters are % and _, among which % Represents any number of characters (including 0 characters), _ represents one character. For example, the following statement is used to query the information of students whose name field contains the word "Zhang":

SELECT * FROM students WHERE name LIKE '%张%'
Copy after login

2. Common operations

The where statement also has many common operations in practical applications. This section will introduce some common operations and their syntax.

  1. AND and OR operations

AND and OR operations are used to connect multiple conditions. AND means that all conditions must be met, and OR means that among multiple conditions As long as one is satisfied. For example, the following statement is used to query the information of students who are under 20 years old and whose math scores are greater than or equal to 90 points:

SELECT * FROM students WHERE age < 20 AND math_score >= 90
Copy after login
  1. NOT operation

NOT operation is used to negate Conditions, for example, the following statement is used to query information about students who are not under 20 years old:

SELECT * FROM students WHERE NOT age < 20
Copy after login
  1. IN and NOT IN operations

The IN operator has been introduced before, it Used to query whether the value of a certain field is in a given set of values. In contrast, the NOT IN operator is used to query whether the value of a field is not in a given set of values. For example, the following statement is used to query the information of students who are not 18, 19, or 20 years old:

SELECT * FROM students WHERE age NOT IN (18, 19, 20)
Copy after login
  1. BETWEEN and NOT BETWEEN operations

The BETWEEN operation has been introduced before symbol, which is used to query whether the value of a certain field is within a given interval. In contrast, the NOT BETWEEN operator is used to query whether the value of a certain field is not within a given interval. For example, the following statement is used to query the information of students who are not between the ages of 18 and 20:

SELECT * FROM students WHERE age NOT BETWEEN 18 AND 20
Copy after login
  1. LIKE and NOT LIKE operations

LIKE has been introduced before Operator, which is used to query data through wildcard matching. In contrast, the NOT LIKE operator is used to exclude data that does not meet the conditions through wildcards. For example, the following statement is used to query the information of students whose name field does not contain the word "Zhang":

SELECT * FROM students WHERE name NOT LIKE '%张%'
Copy after login
  1. IS NULL and IS NOT NULL operations

IS NULL operation operator is used to query whether the value of a certain field is NULL, and the IS NOT NULL operator is used to query whether the value of a certain field is NOT NULL. For example, the following statement is used to query the information of students who have not filled in their phone numbers:

SELECT * FROM students WHERE phone_number IS NULL
Copy after login
  1. EXISTS and NOT EXISTS operations

EXISTS and NOT EXISTS operators are used to query whether There is a result set of a subquery statement, which is often used to query whether there is data that meets the conditions in a certain table. For example, the following statement is used to query whether there is student information with a certain ID:

SELECT * FROM students WHERE EXISTS (SELECT * FROM students WHERE id = '001')
Copy after login

The above is an introduction to the basic syntax and common operations of the where statement in the PHP query statement. I hope it will be helpful to everyone.

The above is the detailed content of where in php query statement. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template