1
제 자신의 능력으로는 임시 테이블을 사용하지 않고이 SQL 기능을 단일 명령문에 작성하려고합니다. 필자의 삶에서 MySQL을 가져 오지 않고 쿼리를 가져올 수 없습니다. "오류 1248 (42000) : 모든 파생 테이블에는 고유 한 별칭이 있어야합니다."유도 된 테이블을 사용하여이 SQL을 단일 명령문으로 표현하는 방법을 설명하십시오.
여기에 유사한 일이있어, 고장 임에도 불구하고, 내가 원하는 무엇 : 도움을
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;
감사 : 여기
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;
내가 임시 테이블을 사용하여 표현하기 위해 노력하고있어의 작업 버전입니다 .