简体   繁体   中英

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 ...

For example: 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:

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).

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".

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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