如果我尝试使用别名(scount,samount),我的sql查询有问题。查询在没有别名的情况下运行正常,并且仅使用第一个(scount)也可以正常工作,但是一旦我添加第二个(samount),查询就无法执行。我认为这与我出于某种原因进行减号查询有关。
任何人都可以帮我解决这个问题,以便我可以使用别名返回值。
select count(t.invoiceID) scount, sum(amount) samount -
IFNULL(
(
select sum(p.amount) as pamount
from invoice t, invoiceFactoring ift, InvoiceType it, Payment p
where t.issuerID = 38
AND ift.invoiceID = t.invoiceID
AND t.invoiceID = p.invoiceID
AND it.invoiceTypeID = t.invoiceTypeID
AND now() > date_add(t.invoiceExpiryDate, INTERVAL 45 DAY)
)
,0)
from invoice t, invoiceFactoring ift, InvoiceType it
where issuerID = 38
AND ift.invoiceID = t.invoiceID
AND it.invoiceTypeID = t.invoiceTypeID
AND now() > date_add(t.invoiceExpiryDate, INTERVAL 45 DAY)
AND t.disabled = 0
AND it.typeCategory = 1
答案 0 :(得分:1)
别名变为。
SELECT 1 blah - 2 <--- wrong
SELECT 1 - 2 blah <--- correct
P.S。澄清
您的查询基本上是这样的:
SELECT ... scount, ... samount - IFNULL(...)
FROM ...
只需用
替换它SELECT ...scount, ... - IFNULL(...) samount
FROM ...
并接受答案,如果它对你有帮助,因为在你开始接受之前你很难得到另一个答案。