基本上我想要添加field1
,field2
和field3
中存储的数字,并且只显示此总数小于field4
的记录...
例如:select * from table where field1 + field2 + field3 <= field 4
但那不起作用......任何帮助表示赞赏
这是我的实际SQL,因为你可以看到它更复杂:
SELECT
*,
SUM ( amountpaid , amountpaid2 , amountpaid3 ) AS total AND
DATE_FORMAT(date_start, ' %d.%m.%Y') AS date_formatted
FROM
calendar_event
WHERE
Date_start BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL 14 DAY) AND
total <= amount )
罗布
答案 0 :(得分:3)
也许你的意思是:
SELECT
*,
amountpaid + amountpaid2 + amountpaid3 AS total,
DATE_FORMAT(date_start, '%d.%m.%Y') AS date_formatted
FROM calendar_event
WHERE Date_start BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL 14 DAY)
AND amountpaid + amountpaid2 + amountpaid3 <= amount
假设您在amountpaid
,amountpaid2
和amountpaid3
中分别付款,这将为您提供在接下来的14天内开始并且未支付过多的所有事件(即支付不足或支付的确实。)
答案 1 :(得分:0)
您的查询
select * from table where field1 + field2 + field3 <= field4
应该完全正常。确保您要寻址的所有列实际存在,并且它们是数字(整数,浮点数,双精度等)。还要确保,如果你真的称之为,那个field4在“field”和“4”之间没有空格 - 你的问题之间有一个空格,比如“field 4”而不是“field4”。
或者,让我们知道您收到的错误消息。
答案 2 :(得分:0)
Select
*,
Coalesce(amountpaid, 0) + Coalesce(amountpaid2, 0) + Coalesce(amountpaid3, 0) As total,
DATE_FORMAT(date_start, ' %d.%m.%Y') As date_formatted
From
calendar_event
Where
Date_start Between Now() And Date_Add(Now(), Interval 14 DAY) And
Coalesce(amountpaid, 0) + Coalesce(amountpaid2, 0) + Coalesce(amountpaid3, 0) <= amount