在组合两个查询时多次重复相同的值

f0ofjuux  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(365)

在执行hole查询时,我在mysql工作台中编写了两个查询,得到了相同的值。我该怎么办?
下面是我的问题

Select * 
from 
  (
    (select division.name,
            date(meter_data.reading_date),
            sum(meter_data.meter_delta) 'Gas Consumption (m3)'
     from meter, meter_data, division
     where meter.meter_id = meter_data.meter_id 
       and meter.division_id = division.division_id
       and Date(meter_data.reading_date) between '2018-04-01' and '2018-05-03' 
     group by date(meter_data.reading_date) 
    ) as table1,

    (Select Date(receipt.receipt_date), 
            sum(case receipt_item.item_name 
                    when 'Gas' then receipt_item.item_amount else 0 
                end) 'Gas Purchase'
     from receipt_item, receipt
     where receipt.receipt_id = receipt_item.receipt_id
       and Date(receipt.receipt_date) between '2018-04-01' and '2018-05-03'  
     group by Date(receipt.receipt_date)
    ) as table2
  );

当分别执行查询时,每个查询返回准确的33行,但组合起来返回1089个rwo
例如,我在这里得到的都是相同数据的重复

但需要这样展示(这是演示图片)

rbpvctlc

rbpvctlc1#

我有一些问题,我的查询如下

Select * 
from 
  (
    (select division.name,
            date(meter_data.reading_date) AS reading_date,
            sum(meter_data.meter_delta) 'Gas Consumption (m3)'
     from meter, meter_data, division
     where meter.meter_id = meter_data.meter_id 
       and meter.division_id = division.division_id
       and Date(meter_data.reading_date) between '2018-04-01' and '2018-05-03' 
     group by date(meter_data.reading_date) 
    ) as table1,

    (Select Date(receipt.receipt_date) AS receipt_date, 
            sum(case receipt_item.item_name 
                    when 'Gas' then receipt_item.item_amount else 0 
                end) 'Gas Purchase'
     from receipt_item, receipt
     where receipt.receipt_id = receipt_item.receipt_id
       and Date(receipt.receipt_date) between '2018-04-01' and '2018-05-03'  
     group by Date(receipt.receipt_date)
    ) as table2
  )
WHERE table1.reading_date = table2.receipt_date;

这解决了我的问题

相关问题