Current Location: Home> Latest Articles> Usage and Optimization of distinct Method in ThinkPHP

Usage and Optimization of distinct Method in ThinkPHP

gitbox 2025-06-28

Concept and Function of Distinct Method

In SQL, the distinct keyword is used to remove duplicate values from query results. In ThinkPHP, the distinct method is used to deduplicate the query results and ensure each record is unique.

Basic Syntax of distinct Method

In ThinkPHP, the distinct method is used to remove duplicates from the query results. The basic syntax is as follows:

$result = Db::name('table_name')->distinct(true)->field('column_name')->select();

Where table_name is the name of the database table, and column_name is the name of the column in the table.

Example Usage of distinct Method

Below is a practical example to demonstrate how the distinct method is used.

Database Structure Setup

First, we need to create a table named "users" in the database with the following structure:

CREATE TABLE `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `age` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Inserting Sample Data

Next, we will insert some sample data into the "users" table:

INSERT INTO `users` (`name`, `age`) VALUES
('Tom', 20),
('Jerry', 22),
('Tom', 20),
('Jerry', 22),
('Mike', 23),
('John', 25);

Using distinct Method to Query Data

Now, we can use the distinct method to query data from the table and remove duplicates:

$result = Db::name('users')->distinct(true)->field('name')->select();

The above code queries the "name" field from the "users" table and removes duplicates from the results.

Displaying Query Results

The query results can be displayed using the following code:

foreach ($result as $value) {
  echo $value['name'] . "<br/>";
}

The output will display the unique names:

Tom
Jerry
Mike
John

Important Notes on distinct Method

Here are a few important things to note when using the distinct method:

distinct Method Only Affects Query Results

The distinct method only affects the query results. It does not modify the actual data in the database.

Parameters of distinct Method

The distinct method can accept a parameter of true to enable deduplication, or false to disable it.

Using distinct with field Method

The distinct method is typically used in combination with the field method to specify which columns to query.

Db::name('table_name')->distinct(true)->field('column_name')->select();

Using distinct with where Clause

If you need to further filter the results, you can combine the distinct method with the where clause:

Db::name('table_name')->distinct(true)->where('column_name', 'value')->select();

This will find and remove duplicates from the rows where the value of the specified column is equal to "value".

Conclusion

This article briefly introduces the usage of the distinct method in ThinkPHP. By using the distinct method, you can effectively remove duplicates from your query results, improving query efficiency and data accuracy. Based on business needs, using the distinct method wisely can be a powerful tool for optimizing SQL queries.