请解释如何使用派生表将此 SQL 表达为单个语句
为了我自己的启发,我尝试在单个语句中编写此 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个:
或更正确的写法:
Try this:
Or more correctly written: