盘点两个表的数据 本帖最后由 lwaif 于 2010-08-04 17:44:16 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 [email protected]_monitor>create table A(id int,count int);Query OK, 0 rows affected (0.01 sec)[email protected]_monitor>create table B like A;Query OK, 0 rows affected (0.00 sec)[email protected]_monitor>insert into A values(1,50),(1,10);Query OK, 2 rows affected (0.00 sec)Records: 2 Duplicates: 0 Warnings: [email protected]_monitor>insert into B values(1,80);Query OK, 1 row affected (0.00 sec)[email protected]_monitor>select first.id,first.count-B.count from (select id,sum(count) as count from A group by id) as first,B where first.id=B.id; +------+---------------------+| id | first.count-B.count |+------+---------------------+| 1 | -20 | +------+---------------------+1 row in set (0.04 sec) select 标题,sum(数量 )from (select 标题,数量 from 表A是盘点数据union allselect 标题,-数量 from 表B是库存数据) tgroup by 标题 mysql 外键 设置了级联删除和更新,还是不能插入 关于mysql数据库恢复? 查询乱码的问题 高分寻求高手写sql语句(用mysql实现) 删除两个表的问题 高手请进 如何在程序安装包中加入MySQL的自动安装程序? 数据库连接出错.............很急很急...在线等 mysql中select...in...的效率 求助:谁有用PHP+MySQL写的论坛的原码? 这段存储过程哪里有问题呀? 如何在存储过程中赋值?
[email protected]_monitor>create table A(id int,count int);
Query OK, 0 rows affected (0.01 sec)[email protected]_monitor>create table B like A;
Query OK, 0 rows affected (0.00 sec)[email protected]_monitor>insert into A values(1,50),(1,10);
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: [email protected]_monitor>insert into B values(1,80);
Query OK, 1 row affected (0.00 sec)[email protected]_monitor>select first.id,first.count-B.count from (select id,sum(count) as count from A group by id) as first,B where first.id=B.id;
+------+---------------------+
| id | first.count-B.count |
+------+---------------------+
| 1 | -20 |
+------+---------------------+
1 row in set (0.04 sec)
from (
select 标题,数量 from 表A是盘点数据
union all
select 标题,-数量 from 表B是库存数据
) t
group by 标题