如何添加整行?我尝试了一切,但没有运气

时间:2017-03-07 13:41:53

标签: php sql

$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";

所谓的输出是这样的: enter image description here

1 个答案:

答案 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
;