简体   繁体   English

Mysql在哪里查询…选择* where field1 + field2 + field3 <=字段4

[英]Mysql where query… select * where field1 +field2 + field3 <= field 4

Basically I want to add up the numbers stored in field1 , field2 and field3 and only display records that this total is less than field4 ... 基本上我想将存储在field1field2field3的数字加起来,并且只显示该总数小于field4 ...

For example: select * from table where field1 + field2 + field3 <= field 4 例如: select * from table where field1 + field2 + field3 <= field 4

But that doesnt work....any help appreciated 但这不起作用....任何帮助表示赞赏

here is my actual SQL as you can see its more complex: 这是我的实际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 )

Rob

Maybe what you mean is: 也许您的意思是:

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

Assuming that you have the separate payments in amountpaid , amountpaid2 and amountpaid3 , this would give you all the events that start in the next 14 days and are not overpaid (ie either underpaid or paid exactly). 假设您分别在amountpaidamountpaid2amountpaid3 ,这将为您提供所有在接下来的14天内开始且未超额支付的事件(即未付或完全付清)。

Your query 您的查询

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

should work totally fine. 应该工作得很好。 Make sure all columns you are addressing actually exist and that they are numeric (integer, float, double, etc.). 确保您要寻址的所有列实际上都存在并且它们是数字(整数,浮点数,双精度数等)。 Also make sure, if you actually called it that, that field4 has no space between "field" and "4" - there is a space between in your question, like "field 4" instead of "field4". 还请确保,如果您实际上这样称呼,那么field4在“ field”和“ 4”之间没有空格-您的问题之间有一个空格,例如“ field 4”而不是“ field4”。

Alternatively let us know what error message you get. 或者,让我们知道您收到什么错误消息。

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 http://sqlfiddle.com/#!2/35f6b/3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM