mysql - 使用 SQL 查询选择数据库数据时出错?

标签 mysql sql

我有这 3 个表:

adhocbills 表:

CREATE TABLE IF NOT EXISTS `adhocbills` 
(
  `sequence` bigint(11) NOT NULL AUTO_INCREMENT,
  `status` varchar(200) NOT NULL,
  `type` varchar(200) NOT NULL,
  `invoice_number` varchar(200) NOT NULL,
  PRIMARY KEY (`sequence`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

INSERT INTO `adhocbills` (`sequence`, `status`, `type`, `invoice_number`) 
VALUES (1, 'Completed', 'Invoice', '1234');

adhocbills_lineitems 表:

CREATE TABLE IF NOT EXISTS `adhocbills_lineitems` 
(
  `sequence` bigint(11) NOT NULL AUTO_INCREMENT,
  `bill_seq` varchar(20) NOT NULL,
  `service` varchar(200) NOT NULL,
  PRIMARY KEY (`sequence`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

INSERT INTO `adhocbills_lineitems` (`sequence`, `bill_seq`, `service`) 
VALUES (1, '1', 'Service 1'), (2, '1', 'Service 2'),
       (3, '1', 'Service 2');

billing_invoices 表:

CREATE TABLE IF NOT EXISTS `billing_invoices` 
(
  `sequence` bigint(20) NOT NULL AUTO_INCREMENT,
  `invoice_number` varchar(200) NOT NULL,
  `sub_total` float NOT NULL,
  `vat_amount` float NOT NULL,
  `grand_total` float NOT NULL,
  `datetime` date NOT NULL,
  `invoice_type` varchar(100) NOT NULL,
  `status` varchar(200) NOT NULL DEFAULT 'Unpaid',
  `total_charges` float NOT NULL,
  `proforma` varchar(1) NOT NULL,
  PRIMARY KEY (`sequence`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

INSERT INTO `billing_invoices` 
(`sequence`, `invoice_number`, `sub_total`, `vat_amount`, `grand_total`, `datetime`, `invoice_type`, `status`, `total_charges`, `proforma`) 
VALUES
(1, '1234', 100, 20, 120, '2016-09-01', 'Invoice', 'Unpaid', 100, '');

我有这个选择查询:

 SELECT 
     COUNT(i.sequence) as counter, 
     l.service as service, 
     SUM(i.sub_total) as sub_total, 
     SUM(i.total_charges) as total_charges, 
     SUM(i.vat_amount) as vat_amount, 
     SUM(i.grand_total) as grand_total 
 FROM 
     billing_invoices i 
 LEFT JOIN 
     adhocbills a ON a.invoice_number = i.invoice_number 
 LEFT JOIN 
     adhocbills_lineitems l ON a.sequence = l.bill_seq 
 WHERE 
     i.proforma <> 'Y' 
     AND i.invoice_type = 'Invoice' 
     AND a.status = 'Completed' 
     AND DATE(i.datetime) >= '2016-09-01' 
     AND DATE(i.datetime) <= '2016-09-30' 
 GROUP BY 
     l.service 
ORDER BY 
     grand_total DESC

我想做的事:

我想从当月的 billing_invoices 中选择所有行,并将此表链接到以下内容:

adhocbills.invoice_number = billing_invoices.invoice_number
adhocbills.sequence = adhocbills_lineitems.bill_seq

并使用现有的 WHERE 子句。

然后我按adhocbills_lineitems.service分组

示例数据显示了这一点:

enter image description here

总计 1035.75,但正确的金额是 1017.26,因此需要额外添加 18.50

额外的 18.50 来自 adhocbills_lineitems 中的行:

bill_seq = 1068
unitprice = 8.50(new column, not important)
quantity = 1(new column, not important)

bill_seq = 1068
unitprice = 10(new column, not important)
quantity = 1(new column, not important)

因此这两行都链接到 adhocbills.sequence = '1068'

似乎当 adhocbills_lineitems 中有多于一行具有不同的 adhocbills_lineitems.service 时,它会重复它

迷你工作示例:

我得到这个结果是:

counter service sub_total total_charges vat_amount grand_total 
2     Service 2   200         200           40         240 
1     Service 1   100         100           20         120

服务 2 所在的行翻倍,不知道为什么?

最佳答案

不要连接表,而是使用子查询连接,该子查询仅返回您关心的唯一列,而不是所有重复项。

SELECT 
     COUNT(i.sequence) as counter, 
     l.service as service, 
     SUM(i.sub_total) as sub_total, 
     SUM(i.total_charges) as total_charges, 
     SUM(i.vat_amount) as vat_amount, 
     SUM(i.grand_total) as grand_total 
     FROM billing_invoices i 
LEFT JOIN adhocbills a ON a.invoice_number = i.invoice_number 
LEFT JOIN (
    SELECT DISTINCT service, bill_seq
    FROM adhocbills_lineitems
) l ON a.sequence = l.bill_seq 
WHERE i.proforma <> 'Y' AND i.invoice_type = 'Invoice' AND a.status = 'Completed' 
    AND DATE(i.datetime) >= '2016-09-01' AND DATE(i.datetime) <= '2016-09-30' 
GROUP BY l.service 
ORDER BY grand_total DESC

关于mysql - 使用 SQL 查询选择数据库数据时出错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39541362/

相关文章:

php - 用表单搜索my​​sqli表并显示结果

php - 在通过本地主机上的浏览器将项目添加到数据库时遇到问题,例如用户之类的项目

SQL查询仅获取另一列中只有1个值的那些行

mysql - Xampp - MySQL 无法创建/写入文件(错误代码 : 2)

mysql - 无法为连接 URL“null”创建类 '' 的 JDBC 驱动程序

php - 从 PHP 向 MYSQL 引入 NULL 字段

sql - 不使用 group by 返回列的总和

mysql - Codeigniter 左外连接

sql - 如何计算一列的累计数字?

php - MYSQL查询显示: the first order for each customer