Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
Pull row with lowest number in a MySQL column?
Use aggregate function MIN() along with GROUP BY for this. Here, we will display the minimum ID for NumberOfProduct . Let us first create a table −
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, NumberOfProduct int ); Query OK, 0 rows affected (0.19 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(NumberOfProduct) values(40); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable(NumberOfProduct) values(40); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable(NumberOfProduct) values(60); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable(NumberOfProduct) values(60); Query OK, 1 row affected (0.06 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+----+-----------------+ | Id | NumberOfProduct | +----+-----------------+ | 1 | 40 | | 2 | 40 | | 3 | 60 | | 4 | 60 | +----+-----------------+ 4 rows in set (0.00 sec)
Following is the query to pull row with lowest number in a column −
mysql> select NumberOfProduct,MIN(Id) from DemoTable group by NumberOfProduct;
This will produce the following output −
+-----------------+---------+ | NumberOfProduct | MIN(Id) | +-----------------+---------+ | 40 | 1 | | 60 | 3 | +-----------------+---------+ 2 rows in set (0.00 sec)
Advertisements
