- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Comparing two columns in a single MySQL query to get one row?
For this, you can use ORDER BY clause. Let us first create a table −
mysql> create table DemoTable -> ( -> Num1 int, -> Num2 int -> ); Query OK, 0 rows affected (0.61 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(60,249); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values(59,250); Query OK, 1 row affected (0.12 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
This will produce the following output −
+------+------+ | Num1 | Num2 | +------+------+ | 60 | 249 | | 59 | 250 | +------+------+ 2 rows in set (0.00 sec)
Here is the query to comparing with two maximum of columns get one row −
mysql> select *from DemoTable -> order by Num2 DESC,Num1 DESC limit 1;
Here is the query to comparing with two maximum of columns get one row −
mysql> select *from DemoTable -> order by Num2 DESC,Num1 DESC limit 1;
Output
This will produce the following output −
+------+------+ | Num1 | Num2 | +------+------+ | 59 | 250 | +------+------+ 1 row in set (0.00 sec)
- Related Articles
- MySQL query to combine two columns in a single column?
- Update two columns with a single MySQL query
- Count two different columns in a single query in MySQL?
- MySQL query to delete a row if two columns are equal
- Order by a function of two columns in a single MySQL query
- MySQL query to sort multiple columns together in a single query
- How to get row count of two tables in different databases in a single query?
- Change multiple columns in a single MySQL query?
- MySQL query to return all items in a single row
- Update multiple columns of a single row MySQL?
- Get the count of two table fields in a single MySQL query?
- Concatenate multiple rows and columns in a single row with MySQL
- MySQL query to get a specific row from rows
- How to get multiple rows in a single MySQL query?
- MySQL query to get the highest value from a single row with multiple columns\n\n\n\n\n\n\n\n\n\n

Advertisements