繁体   English   中英

SQL将结果集中的列“合并”为一个结果集

[英]SQL 'merging' columns from result sets into one result set

我一直在讨论如何在MySQL的约束范围内编写特定视图。

下表和列很重要:

CREATE TABLE `invoices` (
  `id` int(10) unsigned NOT NULL AUTO INCREMENT,
  PRIMARY KEY (`id`)
)

-- Joins payments to invoices. The sum of all `invoice_currency_value`s is the balance paid towards an invoice.
CREATE TABLE `financial_transactions_invoices` (
  `id` int(10) unsigned NOT NULL AUTO INCREMENT,
  `invoice` int(10) unsigned NOT NULL,
  `invoice_currency_value` decimal(8,2) unsigned NOT NULL,
  PRIMARY KEY (`id`)
)

-- Lists items (services) available to purchase.
CREATE TABLE `items` (
  `id` int(10) unsigned NOT NULL AUTO INCREMENT,
  `value` decimal(8,2) unsigned NOT NULL
  PRIMARY KEY (`id`)
)

-- Each instance represents that the `item` has been purchased.
CREATE TABLE `item_instances` (
  `id` int(10) unsigned NOT NULL AUTO INCREMENT,
  `invoice` int(10) unsigned NOT NULL,
  `item` int(10) unsigned NOT NULL,
  `invoice_currency_rate` decimal(11,5) unsigned NOT NULL,
  PRIMARY KEY (`id`)
)

-- Any number of tax instances can exist for an item instance and indicate this tax has been applied to the associated item instance.
CREATE TABLE `tax_instances` (
  `id` int(10) unsigned NOT NULL AUTO INCREMENT,
  `item_instance` int(10) unsigned NOT NULL,
  `value` decimal(8,2) unsigned NOT NULL,
  PRIMARY KEY (`id`)
)

现在,我需要一个为每一行列出的视图,

  • 发票号码
  • 发票总值
  • 发票上的总税额
  • 以及支付给发票的总金额

但是,我无法弄清楚如何将这三个单独的查询放入同一结果集(每张发票一行),例如

inv_no  total_value     total_tax       payments
1       150             5               120
2       120             10              20
3       10              0               10
4       1000            150             1150

我写了下面的查询,它产生了想要的结果,但是由于MySQL视图中的“ no subquery”规则,这是不可接受的。

SELECT `invoice_id`, SUM(`total_value`) AS `total_value`, SUM(`total_tax`) AS `total_tax`,
    SUM(`paid_balance`) AS `paid_balance`
FROM
(SELECT `invoices`.`id` AS `invoice_id`, SUM(`items`.`value` * `item_instances`.`invoice_currency_rate`) AS `total_value`,
    NULL AS `total_tax`, NULL AS `paid_balance`
FROM `items`
    JOIN `item_instances` ON `items`.`id` = `item_instances`.`item`
    JOIN `invoices` ON `item_instances`.`invoice` = `invoices`.`id`
GROUP BY `invoices`.`id`
UNION
SELECT `invoices`.`id`, NULL, SUM(`tax_instances`.`value`), NULL
FROM `tax_instances`
    JOIN `item_instances` ON `tax_instances`.`item_instance` = `item_instances`.`id`
    JOIN `invoices` ON `item_instances`.`invoice` = `invoices`.`id`
GROUP BY `invoices`.`id`
UNION
SELECT `invoices`.`id`, NULL, NULL, SUM(`financial_transactions_invoices`.`invoice_currency_value`)
FROM `financial_transactions_invoices`
    JOIN `invoices` ON `financial_transactions_invoices`.`invoice` = `invoices`.`id`
GROUP BY `invoices`.`id`) AS `components`
GROUP by `invoice_id`;

如果不以我现有的方式解决问题,就无法想到可以在MySQL中完成此任务的任何其他方式。

有任何想法吗? 感谢任何帮助。

您可以创建两个视图。 一种使用UNION子查询,另一种使用外部查询。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM