Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.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 根据产品id获取salesQty之和_Mysql - Fatal编程技术网

Mysql 根据产品id获取salesQty之和

Mysql 根据产品id获取salesQty之和,mysql,Mysql,我有以下疑问: select pt_product_name, (select sum(sal_qty) from sales_tb as sal where pt.pt_productid=sal.sal_pt_productid and sal.sal_updated_time>curdate() ) as salQty from product_tb as pt 它返回每个产品的sal_数量的sum,但我需要基于不同产品的sum。 你能告诉我正

我有以下疑问:

select pt_product_name,
  (select 
     sum(sal_qty)
   from sales_tb as sal
   where pt.pt_productid=sal.sal_pt_productid
   and sal.sal_updated_time>curdate()
  ) as salQty
from product_tb as pt
它返回每个产品的sal_数量的
sum
,但我需要基于不同产品的
sum

你能告诉我正确的方向吗?我错过了什么?谢谢。

在select语句中添加
GROUP BY pt.pt\U productid
,将
sum()
应用于产品组

也应考虑使用<代码>连接< /代码>,而不是嵌套的选择。

更新

select pt_product_name, 
  (select sum(sal_qty) from sales_tb as sal
    where pt.pt_productid=sal.sal_pt_productid
    and sal.sal_updated_time>curdate()
  ) as salQty
  from product_tb as pt
  group by pt.pt_productid; 
使用
加入

select pt_product_name, sum(sal_qty) as salQty
  from product_tb as pt
  left outer join sales_tb sal on (pt.pt_productid = sal.sal_pt_productid 
      and sal.sal_updated_time>curdate())
  group by pt.pt_productid
下面是一个示例表结构,其中填充了示例值,并演示了查询工作正常:

    DROP TABLE IF EXISTS `temp_db`.`product_tb`;
    CREATE TABLE  `temp_db`.`product_tb` (
        pt_productid int(10) not null auto_increment,
        pt_product_name varchar(20) not null,
        primary key (`pt_productid`)
    ) ENGINE=INNODB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
    DROP TABLE IF EXISTS `temp_db`.`sales_tb`;
    CREATE TABLE  `temp_db`.`sales_tb` (
        sal_saleid int(10) not null auto_increment,
        sal_pt_productid int(10) not null references product_tb.pt_productid,
        sal_qty int(10) default 0,
        sal_updated_time timestamp not null default current_timestamp,
        primary key (`sal_saleid`)
    ) ENGINE=INNODB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
将示例数据添加到表中:

    insert into product_tb(pt_product_name) values 
        ('prod1'), ('prod2'), ('prod3'), ('prod4');
    insert into sales_tb(sal_pt_productid, sal_qty) values 
        (1, 40), (1, 40), (1, 10), (1, 20),
        (2, 4), (2, 4), (2, 4), (2, 4), 
        (3, 1), (3, 1), (3, 1),
        (4, 5), (4, 5);
select语句的结果(两者都返回相同的结果集):

注意,只有在今天或以后售出的情况下,才会对数量进行汇总<代码>sal_更新时间>curdate()

如果某个产品没有curdate()或更高版本的任何销售,它甚至不会出现在列表中。这并不意味着选择错误,这意味着您没有正确指定目标

select 
    pt.pt_product_name,
    sum(sal.sal_qty) as salQty
from
    product_tb as pt
        left join
    sales_tb as sal on pt.pt_productid = sal.sal_pt_productid
where
    sal.sal_updated_time > curdate()
group by pt.pt_productid

此查询将所有产品与其各自的销售连接起来。然后将同一产品的所有记录分组在一起。然后根据当前日期过滤记录。这些组中的每一组将包含单个产品每次销售的一个记录。最后,在select we sum(选择我们求和)中,为每个组提供所有销售数量。

请提供任何示例我有三种产品。但它显示一种产品和求和(allprdqty)。请使用您的表结构和当前搜索语句(仅给出一行)对您的问题进行更新。此外,列出的产品的求和是否正确?还是仍在返还全部金额?在第一种情况下,将更新时间检查从
JOIN
移动到主
WHERE
子句。是的,必须列出产品。但是第一个产品数量上显示的sum(allprdqty)给出的答案与产品名称和sum(allprdqty)类似。但是我希望prdname sum(prdqty)sum(…)仅应用于每个组(因为group by)。每个组包含单个产品的所有销售额。因此,它将为每个产品返回一行。每一行将包含产品名称以及该产品的销售次数。同样,我有相同的问题我有三个产品。但它显示一个产品和总和(allprdqty),因为sal.sal_updated_time>curdate()除非至少有一个销售满足该条件,否则结果中不会显示产品。请使用代码格式,并尝试生成具有预期输出的数据样本。
select 
    pt.pt_product_name,
    sum(sal.sal_qty) as salQty
from
    product_tb as pt
        left join
    sales_tb as sal on pt.pt_productid = sal.sal_pt_productid
where
    sal.sal_updated_time > curdate()
group by pt.pt_productid