简体   繁体   English

mysql group_concat 和 group by 不工作

[英]mysql group_concat and group by not working

I have the following two tables (simplified):我有以下两个表(简化):

tblinvoices tblinvoices

id  dateCreated    

1   2020-01-02
2   2020-01-03
3   2020-01-03
4   2020-01-04

tblinvoicesLines tblinvoicesLines

id invoiceId vatId vatAmount
1  4         7     35.50
2  4         8     15.75
3  4         7     11.50
3  5         7     10.05
4  6         7     11.04

I am trying to aggregate the vatAmount results in the output, like for this example:我正在尝试在输出中汇总 vatAmount 结果,例如此示例:

invoiceId   listVatAmounts
4          47.00,15.75
5          10.05
6          11.04

I have the following mysql query:我有以下 mysql 查询:

select
tblInvoices.id as invoiceId,

(
select group_concat(vatAmount) from tblinvoicesLines
where tblinvoicesLines.invoiceId=tblInvoices.id
group by tblinvoicesLines.vatId
) as listVatAmounts

from tblInvoices
order by id desc
limit 10

I was hoping the group by tblinvoicesLines.vatId would group these values together, but mysql returns an error:我希望tblinvoicesLines.vatId将这些值组合在一起,但 mysql 返回错误:

Subquery returns more than 1 row子查询返回超过 1 行

What can I do to achieve the desired result?我该怎么做才能达到预期的结果? I want it to be achieved via a subquery.我希望它通过子查询来实现。

You need to perform the sums by vatId first, and then you can list the vatAmount sums by invoiceId :您需要vatId执行总和,然后您可以按invoiceId列出vatAmount总和:

SELECT invoiceId, GROUP_CONCAT(sumVatAmounts) AS listVatAmounts
FROM (SELECT invoiceId, vatId, SUM(vatAmount) AS sumVatAmounts
      FROM tblinvoicesLines
      GROUP BY invoiceId, vatId) t
GROUP BY invoiceId

Output:输出:

invoiceId   listVatAmounts
4           47.00,15.75
5           10.05
6           11.04

If you need to include values from tblInvoices , you can just JOIN that table to the above query as a derived table:如果您需要包含来自tblInvoices值,您可以将该表作为派生表JOIN到上述查询中:

SELECT i.id AS invoiceId, i.dateCreated, GROUP_CONCAT(t.sumVatAmounts) AS listVatAmounts
FROM tblInvoices i
LEFT JOIN (SELECT invoiceId, vatId, SUM(vatAmount) AS sumVatAmounts
           FROM tblinvoicesLines
           GROUP BY invoiceId, vatId) t ON t.invoiceId = i.id
GROUP BY i.id, i.dateCreated

Or if you really must use a subquery:或者,如果您确实必须使用子查询:

SELECT i.id AS invoiceId,
       (SELECT GROUP_CONCAT(sumVatAmounts)
        FROM (SELECT invoiceId, vatId, SUM(vatAmount) AS sumVatAmounts
              FROM tblinvoicesLines
              GROUP BY invoiceId, vatId) t
        WHERE t.invoiceId = i.id) AS listVatAmounts
FROM tblInvoices i

Demo on dbfiddle dbfiddle 上的演示

You can also do it with one SELECT like this:你也可以用这样的一个SELECT来做到这一点:

SELECT  invoiceId, CONCAT(
    SUM(IF(vatId=7,vatAmount,0))
    ,if( SUM(IF(vatId=8,vatAmount,0)) >0
        , CONCAT(',',SUM(IF(vatId=8,vatAmount,0)))
        ,'')
    ) AS listVatAmounts
FROM invoiceId
GROUP BY invoiceId;

OR with subquery and for all invoiceIds OR 与子查询和所有 invoiceIds

SELECT
    invoiceId,
    GROUP_CONCAT(vatAmount ORDER BY vatId SEPARATOR ', ') AS listVatAmounts
FROM (
    SELECT id, invoiceId, vatId, SUM(vatAmount) as vatAmount
    FROM invoiceId
    GROUP BY invoiceId,vatId
    ) grp
GROUP BY invoiceId
ORDER BY id;

Sample样本

MariaDB [bernd]> SELECT  invoiceId, CONCAT(
    ->     SUM(IF(vatId=7,vatAmount,0))
    ->     ,if( SUM(IF(vatId=8,vatAmount,0)) >0
    ->         , CONCAT(',',SUM(IF(vatId=8,vatAmount,0)))
    ->         ,'')
    ->     ) AS listVatAmounts
    -> FROM invoiceId
    -> GROUP BY invoiceId;
+-----------+----------------+
| invoiceId | listVatAmounts |
+-----------+----------------+
|         4 | 47.00,15.75    |
|         5 | 10.05          |
|         6 | 11.04          |
+-----------+----------------+
3 rows in set (0.01 sec)

MariaDB [bernd]>

Sample 2样本 2

MariaDB [bernd]> SELECT
    ->     invoiceId,
    ->     GROUP_CONCAT(vatAmount ORDER BY vatId SEPARATOR ', ') AS listVatAmounts
    -> FROM (
    ->     SELECT id, invoiceId, vatId, SUM(vatAmount) as vatAmount
    ->     FROM invoiceId
    ->     GROUP BY invoiceId,vatId
    ->     ) grp
    -> GROUP BY invoiceId
    -> ORDER BY id;
+-----------+----------------+
| invoiceId | listVatAmounts |
+-----------+----------------+
|         4 | 47.00, 15.75   |
|         5 | 10.05          |
|         6 | 11.04          |
+-----------+----------------+
3 rows in set (0.00 sec)

MariaDB [bernd]>

What Ive done is the following:我所做的是以下内容:

select
@invoiceId:= tblInvoices.id as invoiceId,

(
select group_concat(vatAmount)
from
(
select sum(vatAmount) as vatAmount
from tblinvoicesLines
where tblinvoicesLines.invoiceId=@invoiceId
group by tblinvoicesLines.vatId
) t
)

as listVatAmounts

from tblInvoices
order by id desc
limit 10

But the performance is not very well.但是性能不是很好。 It does it however in one subquery然而,它是在一个子查询中完成的

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

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