Home >Database >Mysql Tutorial >How to Select Records with the Minimum Field Value in MySQL?

How to Select Records with the Minimum Field Value in MySQL?

Patricia Arquette
Patricia ArquetteOriginal
2024-10-26 06:59:30906browse

How to Select Records with the Minimum Field Value in MySQL?

Retrieving Records with Minimum Field Values in MySQL

In MySQL, you may encounter scenarios where you need to select data from a table based on a field that possesses the minimum value. To achieve this, consider the following approaches:

One common method is to employ the MIN() aggregate function:

SELECT * FROM pieces WHERE price = MIN(price)

However, this query retrieves all records with the same minimum price, providing you with multiple rows. For a more precise result, you can utilize the following:

SELECT *
FROM pieces
WHERE price = (SELECT MIN(price) FROM pieces)

In this query, the subquery (SELECT MIN(price) FROM pieces) fetches the minimum price from the pieces table. The outer query then selects all records whose price field matches this minimum value.

Here's a practical demonstration using SQLFiddle:

[SQLFiddle Demo](https://www.sqlfiddle.com/#!9/b3ac32/26)

The above is the detailed content of How to Select Records with the Minimum Field Value in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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