我有一个查询。我将两个列的值添加到一个派生的列,然后我想从派生的列和已经存在的列中求出差值。
但是我遇到了一些未知的列错误
SELECT `st_id` , `fee` , SUM( `total` ) AS total, SUM( `books` ) AS book,SUM( `uniform` ) AS uniform,SUM( `total` + `books` + `uniform` ) AS amount,SUM( `amount` - `paid` ) AS pending
FROM `fee_tbl`
WHERE `st_id`
IN ( 40, 504, 533, 640, 817, 944 )
GROUP BY `fee` , `st_id`
有人知道怎么了吗?
最佳答案
如果要为特定的amount
和pending
查找fee
和st_id
然后
SELECT
`st_id` ,
`fee` ,
SUM( `total` ) AS total,
SUM( `books` ) AS book,
SUM( `uniform` ) AS uniform,
SUM( `total` + `books` + `uniform` ) AS amount,
SUM( `total` + `books` + `uniform` - `paid` ) AS pending
FROM `fee_tbl`
WHERE `st_id` IN ( 40, 504, 533, 640, 817, 944 )
GROUP BY `fee` , `st_id`
要么
SELECT
`st_id` ,
`fee` ,
SUM( `total` ) AS total,
SUM( `books` ) AS book,
SUM( `uniform` ) AS uniform,
SUM( `total` ) + SUM( `books` ) + SUM( `uniform` ) AS amount,
SUM( `total` ) + SUM( `books` ) + SUM( `uniform` ) - SUM(`paid` ) AS pending
FROM `fee_tbl`
WHERE `st_id` IN ( 40, 504, 533, 640, 817, 944 )
GROUP BY `fee` , `st_id`