Find the only row with the minimum value in SQLWhen processing the data in the relationship database, it is usually necessary to retrieve a line with minimum value in a specific column. This is especially useful when you want to identify the minimum value associated with different identifiers.
To achieve this purpose in SQL, you can use the following methods:
This query effectively retrieves all lines with the minimum point value of the minimum point value of its respective identification (ID) of the Tableneame table (alias TBL).
<code class="language-sql">SELECT tbl.* FROM TableName tbl INNER JOIN ( SELECT Id, MIN(Point) MinPoint FROM TableName GROUP BY Id ) tbl1 ON tbl1.id = tbl.id WHERE tbl1.MinPoint = tbl.Point</code>Copy after loginLet's use the table provided to explain this concept:
Execute the query with this table will produce the following results:
<code>id game point 1 x 5 1 z 4 2 y 6 3 x 2 3 y 5 3 z 8</code>Copy after loginAs you can see, the query successfully identifies those lines with minimum dots in each game.
<code>id game point 1 z 4 2 y 6 3 x 2 </code>Copy after loginNow, whenever you need to find those rows with the lowest value in specific columns (groups according to one or more identifiers), this technology can be used.
The above is the detailed content of How to Find Distinct Rows with Minimum Values in SQL?. For more information, please follow other related articles on the PHP Chinese website!