- 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
How can we subtract values in MySQL table with the help of LEFT JOIN?
It can be understood with the help of an example in which two tables are having some values and we subtract the values with the help of LEFT JOIN. Here we are taking two tables having the following data −
mysql> Select * from value_curdate; +----+----------+-------+ | Id | Product | Price | +----+----------+-------+ | 1 | Notebook | 100 | | 2 | Pen | 40 | | 3 | Pencil | 65 | +----+----------+-------+ 3 rows in set (0.00 sec) mysql> Select * from value_prevdate; +----+-----------+-------+ | Id | Product | Price | +----+-----------+-------+ | 1 | Notebook | 85 | | 2 | Pen | 34 | | 3 | Pencil | 56 | | 4 | Colors | 65 | | 5 | Fevistick | 25 | +----+-----------+-------+ 5 rows in set (0.00 sec)
The above two tables are having the current price and previous price of products respectively. Now the following query, by using LEFT JOIN, will find the difference in price between the same products stored in both the tables.
mysql> Select value_curdate.id, value_curdate.product, value_curdate.price as Curprice,value_prevdate.price as 'prevprice', value_curdate.price-value_prevdate.price as 'Difference' from value_curdate LEFT JOIN value_prevdate ON value_curdate.id = value_prevdate.id ; +----+----------+----------+-----------+------------+ | id | product | Curprice | prevprice | Difference | +----+----------+----------+-----------+------------+ | 1 | Notebook | 100 | 85 | 15 | | 2 | Pen | 40 | 34 | 6 | | 3 | Pencil | 65 | 56 | 9 | +----+----------+----------+-----------+------------+ 3 rows in set (0.00 sec)
- Related Articles
- How can we create a MySQL view with LEFT JOIN?
- How can we insert values into a table with the help of MySQL self-computed output?
- How can we write MySQL query for inner joins with the help of keyword JOIN?
- How can we write MySQL query for cross joins with the help of keyword JOIN?
- How to remove duplicate values from a MySQL table using LEFT JOIN?
- How can we update the values in one MySQL table by using the values of another MySQL table?
- How can we display all the records from MySQL table with the help of PHP script?
- How can we convert subqueries to LEFT JOIN?
- What is MySQL LEFT JOIN and how can we write MySQL query for it?
- How can we update values in a MySQL table?
- How can we filter data with the help of MySQL subquery?
- How can we create a MySQL view with INNER JOIN?
- How can we create a MySQL view with RIGHT JOIN?
- How can we update any value in MySQL view as we can update the values in MySQL table?
- How can we add values into the columns of a MySQL table?

Advertisements