Home > PHP Framework > ThinkPHP > How to query with multiple conditions in thinkphp

How to query with multiple conditions in thinkphp

WBOY
Release: 2022-04-06 14:38:52
Original
3965 people have browsed it

In thinkphp, you can use "_complex" to query multiple conditions. "_complex" is often used to implement compound queries. You can set an "AND" relationship between the first query and the second query condition. , the syntax is "condition one ['_complex'] = condition two".

How to query with multiple conditions in thinkphp

The operating environment of this article: Windows 10 system, ThinkPHP version 5, Dell G3 computer.

How to query multiple conditions in thinkphp

Open the thinkphp controller file and create a function is_power for testing.

Create the first query condition and save it in the $where array. For example, query the records with id equal to 1. Create a second query condition, and use _logic to set the query condition to be an "OR" relationship, and save it in the $map array. For example, the adopt field value is not equal to 2 or type_id is equal to 24.

Then set the first query condition and the second query condition to be "AND" relationship through _complex.

How to query with multiple conditions in thinkphp

In the is_power function, use find() to query data that meets the conditions, and use getLastSql() to output the generated sql.

How to query with multiple conditions in thinkphp

Open the file execution controller in the browser and view the generated sql.

How to query with multiple conditions in thinkphp

Summary:

1. In the thinkphp controller, the second query condition cooperates with _logic to set the logical relationship.

2. The relationship between the first query condition and the second query condition uses _complex to realize the logical relationship of "AND". thinkphp

Notes

The second query condition must be defined in a new array.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to query with multiple conditions in thinkphp. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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