可以透過一個範例來理解,其中有兩個表具有一些值,我們使用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中文網其他相關文章!