惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

B
Blog RSS Feed
量子位
Recent Announcements
Recent Announcements
T
The Blog of Author Tim Ferriss
美团技术团队
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Blog — PlanetScale
Blog — PlanetScale
H
Help Net Security
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - Franky
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
宝玉的分享
宝玉的分享
大猫的无限游戏
大猫的无限游戏
V
Visual Studio Blog
博客园 - 聂微东
aimingoo的专栏
aimingoo的专栏
Microsoft Security Blog
Microsoft Security Blog
U
Unit 42
J
Java Code Geeks
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
IT之家
IT之家
Hugging Face - Blog
Hugging Face - Blog
腾讯CDC
L
LangChain Blog

博客园 - 南岗V哥

# 一个不一样的小程序开发教程1 # 阿里云中的mysql,Ubuntu下的口令初始修改方法 按此格式写你的distributionUrl,可以直接用本地的gradle包 关于雅思短文助手的需求描述 设置Ubuntu屏幕分辨率,1404 设置Ubuntu右侧显示扩展屏幕。。。 家庭版Windows10没有远程桌面的问题 zt secureCRT serialNo - 南岗V哥 ZT 解决Ubuntu下SublimeText3不能输入中文问题 ubuntu下,python2.7安装mysqlldb驱动方法 ZT 将sublime text的tab改为四个空格 ZT Linux可用的最新版本的sublime text注册 - 南岗V哥 http/ftp等的URL匹配正则表达式 ZT 国内163的Ubuntu更新源 oracle11g的监听配置文件中的program和env两个配置,必须干掉,客户端才能正常连接 ubuntu下安装php7 oracle密码过期的修改 oracle 查看字段说明 解决ubuntu下firefox的flash全屏后不能恢复问题 ZT Fedora 23 U盘启动出现“Failed to load ldlinux.c32”解决 ZT vs2015和Oracle在一起时的Shit问题
今天有人在群里问库存的事,顺手写了一个求库存的,Mysql版本
南岗V哥 · 2019-07-30 · via 博客园 - 南岗V哥

建三张表,goods,goods_in,goods_out,分别代表商品表,商品进货表,商品出库表

DROP TABLE IF EXISTS `goods`;
CREATE TABLE `goods` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `goods_title` varchar(255) DEFAULT NULL,
  `price_in` decimal(20,2) DEFAULT NULL,
  `price_out` decimal(20,2) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of goods
-- ----------------------------
INSERT INTO `goods` VALUES ('1', 'A', '80.00', '100.00');
INSERT INTO `goods` VALUES ('2', 'B', '89.00', '100.00');
INSERT INTO `goods` VALUES ('3', 'C', '50.00', '80.00');
INSERT INTO `goods` VALUES ('4', null, null, null);

-- ----------------------------
-- Table structure for goods_in
-- ----------------------------
DROP TABLE IF EXISTS `goods_in`;
CREATE TABLE `goods_in` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `goods_id` int(11) DEFAULT NULL,
  `amount` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of goods_in
-- ----------------------------
INSERT INTO `goods_in` VALUES ('1', '1', '90');
INSERT INTO `goods_in` VALUES ('2', '2', '300');
INSERT INTO `goods_in` VALUES ('3', '3', '100');
INSERT INTO `goods_in` VALUES ('4', '1', '50');
INSERT INTO `goods_in` VALUES ('5', '3', '30');

-- ----------------------------
-- Table structure for goods_out
-- ----------------------------
DROP TABLE IF EXISTS `goods_out`;
CREATE TABLE `goods_out` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `goods_id` int(11) DEFAULT NULL,
  `amount` int(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of goods_out
-- ----------------------------
INSERT INTO `goods_out` VALUES ('1', '1', '3');
INSERT INTO `goods_out` VALUES ('2', '2', '4');
INSERT INTO `goods_out` VALUES ('3', '1', '1');
INSERT INTO `goods_out` VALUES ('4', '3', '8');

下面来求库存。。。。。

SELECT
    t3.id,
    t3.goods_title,
    (t1.in_amount - t2.out_amount) storge_amount
FROM
    (
        SELECT
            goods_id,
            sum(amount) in_amount
        FROM
            goods_in a
        GROUP BY
            goods_id
    ) t1,
    (
        SELECT
            goods_id,
            sum(amount) out_amount
        FROM
            goods_out a
        GROUP BY
            goods_id
    ) t2,
    goods t3
WHERE
    t1.goods_id = t2.goods_id
AND t1.goods_id = t3.id