$sql_main = "SELECT artistcd.cdID,artistcd.cdTitle,artistcd.cdPrice, sum(artistCD.cdPrice * sdetails.qty) AS TotalSales,
(SELECT SUM(TotalSales)) FROM artistcd NATURAL JOIN sdetails WHERE artistID = $artistID";
答案 0 :(得分:0)
结合数据库中的可能性以及您可能想要实现的目标,我提出了以下内容。但是,您的数据库必须支持WITH子句。
WITH
detail AS (
SELECT
artistcd.cdID
, artistcd.cdTitle
, artistcd.cdPrice
, sum(artistcd.cdPrice * sdetails.qty) AS TotalSales
FROM artistcd
NATURAL
JOIN sdetails
WHERE artistID = $ARTISTID
)
SELECT
*
FROM detail
UNION ALL
SELECT
999999999999 AS cdID
, 'All Titles' AS cdTitle
, CAST(NULL AS INT) AS cdPrice
, SUM(TotalSales) AS TotalSales
FROM detail
ORDER BY 1
;