

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to retrieve a value with MySQL count() having maximum upvote value?
Let’s say we have some columns in the table, one for image path and another for the upvotes. However, the first column is the auto increment Id as shown below −
mysql> create table DemoTable( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,ImagePath varchar(100),UpvoteValue int ); Query OK, 0 rows affected (0.72 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(ImagePath,UpvoteValue) values('Image1.jpeg',90); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable(ImagePath,UpvoteValue) values('Image2.jpeg',10); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable(ImagePath,UpvoteValue) values('Image3.jpeg',120); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable(ImagePath,UpvoteValue) values('Image4.jpeg',114); Query OK, 1 row affected (1.17 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+----+-------------+-------------+ | Id | ImagePath | UpvoteValue | +----+-------------+-------------+ | 1 | Image1.jpeg | 90 | | 2 | Image2.jpeg | 10 | | 3 | Image3.jpeg | 120 | | 4 | Image4.jpeg | 114 | +----+-------------+-------------+ 4 rows in set (0.00 sec)
Here is the query to retrieve imagepath value with count() having max upvote value −
mysql> select ImagePath from DemoTable where UpvoteValue IN (select max(UpvoteValue) from DemoTable);
This will produce the following output >
+-------------+ | ImagePath | +-------------+ | Image3.jpeg | +-------------+ 1 row in set (0.00 sec)
- Related Questions & Answers
- How to retrieve the corresponding value for NULL with a MySQL query?
- How to count the number of columns having specific value in MySQL?
- Count rows having three or more rows with a certain value in a MySQL table
- Prevent having a zero value in a MySQL field?
- How to check whether column value is NULL or having DEFAULT value in MySQL?
- Limiting numbers to a maximum value in MySQL?
- Count number of subsets having a particular XOR value in C++
- MySQL select count by value?
- Retrieve records whenever a column value starts with 2 vowel letters in MySQL
- How to get the count of a specific value in a column with MySQL?
- Two ways to fetch maximum value from a MySQL column with numbers
- How to get the maximum value from strings with integers in MySQL?
- MySQL SELECT to sum a column value with previous value
- Get three records having higher value from MySQL
- How to retrieve the value from a table cell with TableModel in Java?
Advertisements