
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Compare Two Columns in a Single MySQL Query
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)
Advertisements