How to select one row with the smallest numerical value of one column?

What will be the query to select one row with the smallest numerical value for one column?

+6
mysql
source share
5 answers

You can use it

SELECT * FROM `YourTable` ORDER BY YourColumn LIMIT 1 
+12
source share

try it

 SELECT * FROM `table` ORDER BY `column` ASC LIMIT 1; 
+5
source share
 select f.* from foo f inner join ( select min(id) as id from foo ) m on m.id = f.id; 
+2
source share

To select all rows that match the minimum value for a single column (in SQL Server)

 SELECT T.col1, T.col2 From Table T Where T.col1 = (select MIN(col1) from Table) 

To select only one row, you can change the first row:

 Select Top 1 T.col1, T.col2 

and you can always add "Order by colx" to the end (several columns separated by commas) too.

Hope this helps.

+1
source share

I was looking for a simliar solution. I need a full row with a mini value for a table group by another column. Here is the solution I came across. I sorted the table using the minium column with the order_by clause and submitted a subquery to the query with ORDER BY, which catches the first row that appears, which is a sorted row.

 id bigint(20) commission decimal(5,4) door_price decimal(19,2) event_id bigint(20) min_commission decimal(19,2) price decimal(19,2) visible_to_public SELECT * FROM (SELECT price_bundle.id as id, event_id, price_bundle.price + (case when ((price_bundle.commission * price_bundle.price) > price_bundle.min_commission) then (price_bundle.commission * price_bundle.price) else price_bundle.min_commission end) AS total FROM price_bundle WHERE price_bundle.visible_to_public = 1 ORDER BY total asc ) AS price_bundle_order_by_total_price_asc GROUP BY event_id 
0
source share

All Articles