Home > Database > Mysql Tutorial > body text

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

Patricia Arquette
Release: 2024-10-26 06:59:30
Original
814 people have browsed it

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)
Copy after login

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)
Copy after login

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!

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!