Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
需要支持MySQL查询吗_Mysql_Sql - Fatal编程技术网

需要支持MySQL查询吗

需要支持MySQL查询吗,mysql,sql,Mysql,Sql,我想要一些关于个人项目的问题的帮助,我自己无法解决。 我试图在一个查询中汇总productId从三个不同表中使用的数量 我想要的SQL结果: 产品ID 总使用量 10 20 20 20 50 100 60 120 似乎您需要表的并集之和 select productId, sum(usedQuantity) usedQuantity from ( select productId, usedQuantity from table_one union all select

我想要一些关于个人项目的问题的帮助,我自己无法解决。 我试图在一个查询中汇总productId从三个不同表中使用的数量

我想要的SQL结果:

产品ID 总使用量 10 20 20 20 50 100 60 120
似乎您需要表的并集之和

select productId, sum(usedQuantity) usedQuantity
from (
   select productId, usedQuantity
   from  table_one
   union all
   select productId, usedQuantity
   from  table_two
   union all
   select productId, usedQuantity
   from  table_three ) t
group by productId

请尝试下面的查询

    select t1.ProductId, Sum(isnull(t1.Quantity,0)+isnull(t2.Quantity,0)+isnull(t3.Quanity,0)) from table1 t1 left join table2 t2 on t1.ProductId=t2,ProductId left join table3 t3 on t1.ProductId=t3.ProductId
group by t1.ProductId
使用和可获得分组列的名称:

创建表:

CREATE TABLE IF NOT EXISTS `t1` (
  `id` int(6) unsigned NOT NULL,
  `productId` int(6) unsigned NOT NULL,
  `usedQuantity` int(6) NOT NULL,
  PRIMARY KEY (`id`,`productId`)
) DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `t2` (
  `id` int(6) unsigned NOT NULL,
  `productId` int(6) unsigned NOT NULL,
  `usedQuantity` int(6) NOT NULL,
  PRIMARY KEY (`id`,`productId`)
) DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `t3` (
  `id` int(6) unsigned NOT NULL,
  `productId` int(6) unsigned NOT NULL,
  `usedQuantity` int(6) NOT NULL,
  PRIMARY KEY (`id`,`productId`)
) DEFAULT CHARSET=utf8;

INSERT INTO `t1` (`id`, `productId`, `usedQuantity`) VALUES
  ('1', '10', '10'),      ('2', '20', '20'),      ('3', '50', '50');
  
INSERT INTO `t2` (`id`, `productId`, `usedQuantity`) VALUES
  ('1', '10', '10'),      ('2', '60', '60');
  
INSERT INTO `t3` (`id`, `productId`, `usedQuantity`) VALUES
  ('1', '50', '50'),      ('3', '60', '60');
  
  
通过构建一个
联合所有这些表的值来查询这些表,然后按
productId
分组并汇总
usedQuantity
列:

select productId, sum(usedQuantity)   # as ... if you like other column header
from (
  select * from t1   # replace * with your field-names to minimizes
  union all          # data retrieved and avoid problems if your table
  select * from t2   # definition change later - see remark by @Akina
  union all          
  select * from t3
) as t_all
group by productId
结果:

productId   sum(usedQuantity)
10          20 
20          20
50          100
60          120
拥有三个完全相同DDL的表对我来说似乎很奇怪,但这是可行的

  • 小提琴:
  • 文档链接到8.0-fiddle是5.6-功能相同

为什么有三个具有相同字段的表?到目前为止,您尝试了什么?您被困在哪里了?将源表数据示例替换为其创建表+插入脚本。例如,我们必须看到does
productId
列定义为unique。这将帮助我完成我的啤酒项目:D干杯@TobiasAxbard不使用星号,请指定单独的列名。。。如果将来某个表的结构将被更改(添加或交换列),查询将失败。请看斯凯塞奇的回答。@akina同意-我懒得使用星号-另一个缺点是它用根本不用的id字段填充联合-专用字段名会更好。编辑。是的,对不起,现在更正了。