可以通过一个例子来理解,其中有两个表具有一些值,我们使用LEFT JOIN来减去这些值。这里我们使用以下数据的两个表:
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)
上述两个表分别存储了产品的当前价格和之前的价格。现在,通过使用LEFT JOIN,以下查询将找到存储在两个表中相同产品之间的价格差异。
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)
以上是我们如何通过LEFT JOIN在MySQL表中减去值?的详细内容。更多信息请关注PHP中文网其他相关文章!