MYSQL查询,用于选择一个表的条件结果并将其显示在另一表上 [英] MYSQL query for selecting the result of a condition of one table and showing it on another table

查看:44
本文介绍了MYSQL查询,用于选择一个表的条件结果并将其显示在另一表上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应从第一行中减去第二行...并将结果显示在第二个表中

The 2nd row should be subtracted from 1st row...and display the result in 2nd table

第一张桌子

--------------------------------
stock_name|temple|quantity|type
--------------------------------
rice      |shiva |  32    |purchase
rice      |shiva |  10    |issue

第二张表最初是空的

我在第二张表中需要的确切内容如下

what exactly i need in 2nd table is as follows

---------------------------
stock_name|temple|quantity
--------------------------
rice      |siva  |22

推荐答案

Insert into Table2(stock_name,temple,quantity)
SELECT a.stock_name, a.temple, SUM(Case when Type='purchase' then quantity else (quantity*-1) End) AS quantity
FROM Table1 a
GROUP BY a.stock_name, a.temple

这篇关于MYSQL查询,用于选择一个表的条件结果并将其显示在另一表上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆