Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/73.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.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,我正在处理查询,该查询工作正常,但在某些条件下不工作。理想情况下,它应该会起作用,但我不知道哪里出了问题。 这里是一个例子,有三个表,如capital machine和qc 查询: select all_dates.value1 as `Date`, coalesce(`Outward1(B_Qty)`,`Outward2(B_Qty)`,`Outward3(B_Qty)`, '0') as `Outward` from (select distinct C_Date as value1

我正在处理查询,该查询工作正常,但在某些条件下不工作。理想情况下,它应该会起作用,但我不知道哪里出了问题。 这里是一个例子,有三个表,如capital machine和qc

查询:

select all_dates.value1 as `Date`, coalesce(`Outward1(B_Qty)`,`Outward2(B_Qty)`,`Outward3(B_Qty)`, '0') as `Outward` 
from 
(select distinct C_Date as value1 from capital where P2_Goods like '750%' and Monthname(C_Date)='April'  
union all
select distinct M_Date from machine  where J_Id like '%750' and Monthname(M_Date)='April' 
union all
select distinct QC_Date from qc where QC_Name like '750%'  and Monthname(QC_Date)='April'  
) as all_dates
left join( select C_Date, sum(PR) AS `Outward1(B_Qty)` 
from capital where P2_Goods like '750%' and Monthname(C_Date)='April' group by C_Date) 
as f on f.C_Date = all_dates.value1 
left join( select M_Date, (sum(PR)+sum(C_Skip)) AS `Outward2(B_Qty)` 
from machine  where J_Id like '%750' and Monthname(M_Date)='April'  group by M_Date) 
as h on  h.M_Date = all_dates.value1 
left join( select QC_Date, sum(PR) AS `Outward3(B_Qty)` 
from qc where QC_Name like '750%' and Monthname(QC_Date)='April' group by QC_Date) 
as k on  k.QC_Date = all_dates.value1 group by all_dates.value1
查询的主要目的是,按日期从每个表组中跳过的PR和C_列的总数
现在,如果两个不同的表上有相同的日期,那么它应该添加PR,但在这种情况下,它不会添加。这就是问题所在。
请帮帮我。提前谢谢。

试试这个

select date1,sum(outward) as outward from
    (
    select c_date as date1,pr as outward from capital
    union all
    select m_date as date1,pr+c_skip  as outward from machine
    union all
    select qc_date as date1,pr  as outward from qc
    ) t
group by date1
试试这个

select date1,sum(outward) as outward from
    (
    select c_date as date1,pr as outward from capital
    union all
    select m_date as date1,pr+c_skip  as outward from machine
    union all
    select qc_date as date1,pr  as outward from qc
    ) t
group by date1

成功了。将复杂查询转换为简单查询。干得好。谢谢。需要学很多东西,这很有效。将复杂查询转换为简单查询。干得好。谢谢。需要学习很多东西。