1. Use the SELECT DISTINCT keyword
In SQL statements, you can use the SELECT DISTINCT keyword to obtain unique data. In ThinkPHP5, when using the query() method of the \think\Db class to execute a SQL query statement, you can directly use the SELECT DISTINCT keyword in the query conditions, for example:
use \think\Db; $data = Db::query('SELECT DISTINCT `brand` FROM `goods` WHERE `category_id` = 1');
In the above code, we use SELECT The DISTINCT keyword obtains all brands with category_id
being 1 in the goods
table, and assigns the query results to the $data variable.
2. Use the distinct() method
In addition to using the SELECT DISTINCT keyword in the SELECT statement, you can also use the distinct() method provided by ThinkPHP5 to achieve deduplication. Inquire. As shown below:
use \think\Db; $data = Db::name('goods') ->where('category_id', 1) ->distinct(true) ->field('brand') ->select();
In the above code, we use the name() method of the Db class to specify the query data table, the where() method to specify the filtering conditions, and the distinct() method The deduplication query is enabled, the field() method is used to specify the query field, and finally the select() method is used to perform the query operation.
3. Use the group() method
In addition to using the SELECT DISTINCT keyword and the distinct() method, you can also use the group() method to implement deduplication queries. As shown below:
use \think\Db; $data = Db::name('goods') ->where('category_id', 1) ->group('brand') ->select();
In the above code, we use the name() method of the Db class to specify the query data table, the where() method to specify the filtering conditions, and the group() method Grouping is performed, and finally the select() method is used to perform the query operation.
In actual development work, we need to choose the appropriate deduplication query method according to specific business needs to ensure the accuracy and efficiency of the query results.
The above is the detailed content of How to remove duplicate queries in thinkphp5. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version
Chinese version, very easy to use

Notepad++7.3.1
Easy-to-use and free code editor