Home > Database > Mysql Tutorial > body text

How Can I Efficiently Select Distinct `gid` Values with a `COUNT(*)` Greater Than 10 in MySQL?

Barbara Streisand
Release: 2024-11-07 11:42:03
Original
962 people have browsed it

How Can I Efficiently Select Distinct `gid` Values with a `COUNT(*)` Greater Than 10 in MySQL?

Utilizing COUNT(*) Effectively in MySQL WHERE Clauses

To achieve the desired result in MySQL, where you aim to select distinct gid values with a COUNT(*) condition exceeding 10 and sort the results in descending order, the approach you suggested using a nested SELECT query within the WHERE clause may not be the most efficient.

Instead, consider employing the GROUP BY and HAVING clauses to accompalish your task. Here's the corrected query:

Understanding the Query

The GROUP BY clause organizes the gd table data by the gid column. This action aggregates rows with the same gid value.

Following that, the HAVING clause applies a filter to the grouped rows, selecting only those with a COUNT(*) greater than 10. This ensures that the returned results meet your specified frequency criteria.

Finally, the ORDER BY clause arranges the remaining rows in descending order based on the lastupdated timestamp, giving you the desired sorting.

The above is the detailed content of How Can I Efficiently Select Distinct `gid` Values with a `COUNT(*)` Greater Than 10 in MySQL?. 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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!