
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
Divide Values of Two Columns Using MySQL Wildcard
Let us first create a table −
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Value1 int, Value2 int ); Query OK, 0 rows affected (0.55 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(Value1,Value2) values(100,150); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(Value1,Value2) values(500,1000); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable(Value1,Value2) values(15000,18000); Query OK, 1 row affected (0.13 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+----+--------+--------+ | Id | Value1 | Value2 | +----+--------+--------+ | 1 | 100 | 150 | | 2 | 500 | 1000 | | 3 | 15000 | 18000 | +----+--------+--------+ 3 rows in set (0.00 sec)
Let us now divide the values from two columns and display the result in a new column with MySQL wildcards −
mysql> select *,(Value1/Value2) AS Result from DemoTable;
This will produce the following output −
+----+--------+--------+--------+ | Id | Value1 | Value2 | Result | +----+--------+--------+--------+ | 1 | 100 | 150 | 0.6667 | | 2 | 500 | 1000 | 0.5000 | | 3 | 15000 | 18000 | 0.8333 | +----+--------+--------+--------+ 3 rows in set (0.00 sec)
Advertisements