-1

基本上我想将存储的数字相加field1field2并且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 )

4

3 回答 3

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这将为您提供在接下来的 14 天内开始且未多付的所有事件(即少付或完全支付)。amountpaid2amountpaid3

于 2012-11-17T01:39:18.960 回答
0

您的查询

select * from table where field1 + field2 + field3 <= field4

应该工作得很好。确保您要寻址的所有列确实存在并且它们是数字的(整数、浮点数、双精度等)。还要确保,如果你真的这么称呼它,那么 field4 在“field”和“4”之间没有空格 - 在你的问题之间有一个空格,比如“field 4”而不是“field4”。

或者让我们知道您收到什么错误消息。

于 2012-11-17T01:16:03.197 回答
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

http://sqlfiddle.com/#!2/35f6b/3

于 2012-11-17T01:36:53.773 回答