请解释如何使用派生表将此 SQL 表达为单个语句

发布于 2024-10-02 17:14:14 字数 1403 浏览 4 评论 0原文

为了我自己的启发,我尝试在单个语句中编写此 SQL 功能,而不使用临时表。在我的一生中,如果没有出现 MySQL“错误 1248 (42000):每个派生表必须有自己的别名”,我就无法让查询正常工作。

这是与我想要的类似的东西,尽管已经损坏了:

SELECT split.share, split.weight, split.creditor, split.debtor, share.amount, wsum.sum
FROM (
    SELECT split.share, split.weight, split.creditor, split.debtor, share.amount
    FROM ( split LEFT JOIN share ON split.share = share.id )
    WHERE debtor = 6 OR creditor = 6 )
    LEFT JOIN (
        SELECT split.share, SUM(weight) AS sum
        FROM split
        GROUP BY split.share
    ) wsum
    ON split.share = wsum.share;

这是我试图使用临时表表达的内容的工作版本:

CREATE TEMPORARY TABLE weightsum (share INT, sum INT);

INSERT INTO weightsum (share, sum)
SELECT split.share, SUM(weight) AS sum
FROM split
GROUP BY split.share;

CREATE TEMPORARY TABLE summary (share INT, weight INT, creditor INT, debtor INT, amount DECIMAL(10,2));

INSERT INTO summary (share, weight, creditor, debtor, amount)
SELECT split.share, split.weight, split.creditor, split.debtor, share.amount
FROM (split LEFT JOIN share ON split.share = share.id)
WHERE debtor = 6 OR creditor = 6;

SELECT summary.share, summary.weight, weightsum.sum, summary.creditor, summary.debtor, summary.amount, ((summary.amount / weightsum.sum) * summary.weight) AS split_amount
FROM summary LEFT JOIN weightsum
ON summary.share = weightsum.share;

感谢您的帮助。

For my own edification, I'm trying to write this SQL functionality in a single statement without using temporary tables. For the life of me, I can't get the query to work without getting a MySQL "ERROR 1248 (42000): Every derived table must have its own alias."

Here's something akin, albeit broken, to what I want:

SELECT split.share, split.weight, split.creditor, split.debtor, share.amount, wsum.sum
FROM (
    SELECT split.share, split.weight, split.creditor, split.debtor, share.amount
    FROM ( split LEFT JOIN share ON split.share = share.id )
    WHERE debtor = 6 OR creditor = 6 )
    LEFT JOIN (
        SELECT split.share, SUM(weight) AS sum
        FROM split
        GROUP BY split.share
    ) wsum
    ON split.share = wsum.share;

Here's a working version of what I'm trying to expressed using temporary tables:

CREATE TEMPORARY TABLE weightsum (share INT, sum INT);

INSERT INTO weightsum (share, sum)
SELECT split.share, SUM(weight) AS sum
FROM split
GROUP BY split.share;

CREATE TEMPORARY TABLE summary (share INT, weight INT, creditor INT, debtor INT, amount DECIMAL(10,2));

INSERT INTO summary (share, weight, creditor, debtor, amount)
SELECT split.share, split.weight, split.creditor, split.debtor, share.amount
FROM (split LEFT JOIN share ON split.share = share.id)
WHERE debtor = 6 OR creditor = 6;

SELECT summary.share, summary.weight, weightsum.sum, summary.creditor, summary.debtor, summary.amount, ((summary.amount / weightsum.sum) * summary.weight) AS split_amount
FROM summary LEFT JOIN weightsum
ON summary.share = weightsum.share;

Thanks for the help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北斗星光 2024-10-09 17:14:14

试试这个:

SELECT split.share, split.weight, split.creditor, split.debtor, share.amount, wsum.sum
FROM (
    SELECT split.share, split.weight, split.creditor, split.debtor, share.amount
    FROM split 
    LEFT JOIN share 
    ON split.share = share.id
    WHERE debtor = 6 OR creditor = 6 
) As split
LEFT JOIN (
    SELECT split.share, SUM(weight) AS sum
    FROM split
    GROUP BY split.share
) wsum
ON split.share = wsum.share;

或更正确的写法:

SELECT split.share, split.weight, split.creditor, split.debtor, share.amount, wsum.sum
FROM split 
LEFT JOIN share 
ON split.share = share.id 
LEFT JOIN (
    SELECT split.share, SUM(weight) AS sum
    FROM split
    GROUP BY split.share
) wsum
ON split.share = wsum.share
WHERE split.debtor = 6 OR split.reditor = 6;

Try this:

SELECT split.share, split.weight, split.creditor, split.debtor, share.amount, wsum.sum
FROM (
    SELECT split.share, split.weight, split.creditor, split.debtor, share.amount
    FROM split 
    LEFT JOIN share 
    ON split.share = share.id
    WHERE debtor = 6 OR creditor = 6 
) As split
LEFT JOIN (
    SELECT split.share, SUM(weight) AS sum
    FROM split
    GROUP BY split.share
) wsum
ON split.share = wsum.share;

Or more correctly written:

SELECT split.share, split.weight, split.creditor, split.debtor, share.amount, wsum.sum
FROM split 
LEFT JOIN share 
ON split.share = share.id 
LEFT JOIN (
    SELECT split.share, SUM(weight) AS sum
    FROM split
    GROUP BY split.share
) wsum
ON split.share = wsum.share
WHERE split.debtor = 6 OR split.reditor = 6;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文