在选择查询中选择查询是可能的

时间:2021-01-20 05:41:10

标签: sql

SELECT account_id, maintenance_cost AS amount FROM community_details 
WHERE community_id = %s

SELECT amount, receipt_no FROM bill_payments WHERE bill_id = %s

我可以合并这两个队列吗

1 个答案:

答案 0 :(得分:0)

是的,您可以使用 UNION

SELECT account_id, maintenance_cost AS amount FROM community_details 
WHERE community_id = %s

UNION

SELECT amount, receipt_no FROM bill_payments WHERE bill_id = %s

例如:

CREATE TABLE rs (`Location` VARCHAR(5), `Region` VARCHAR(5), `Year` VARCHAR(5), `Sales` INTEGER);

INSERT INTO rs (`Location`, `Region`, `Year`, 'Sales') VALUES
  ('SS1', 'R1', '2019', 100),
  ('SS2', 'R1', '2019', 200),
  ('SS3', 'R2', '2019', 300),
  ('SS1', 'R1', '2020', 400),
  ('SS2', 'R1', '2020', 500),
  ('SS3', 'R2', '2020', 600);

with res as (select * from rs
where Year='2020')

select Location as Location , sum(sales) as "Sales(2020)" from res
group by Location
UNION
select Region, sum(sales) from res
group by region
UNION
select "Total", sum(sales) from res

相关问题