开发者

Calculate Total in a Given Row

开发者 https://www.devze.com 2023-01-11 18:15 出处:网络
I am developin开发者_如何转开发g a system where i need this: I have various rows on one table, and one of them is Total

I am developin开发者_如何转开发g a system where i need this:

  • I have various rows on one table, and one of them is Total
  • I want the script that can add the total of all the fields in the Total column.
  • I will then add the session user so that it only calculates all total fields in that column for that particular person.

You can get me the query: Example the search query for 30 days is this

$query="SELECT * 
          FROM clientdata 
         WHERE ADDTime BETWEEN DATE_SUB(NOW(), INTERVAL 30 DAY) AND NOW() 
           AND member_id='" . $_SESSION['SESS_FIRST_NAME'] . "'";

So you can also add the session details on the code you post below


I don't know if I got it right but I think you want to do this

select sum(total) WHERE ADDTime BETWEEN DATE_SUB(NOW(), INTERVAL 30 DAY) AND NOW() AND member_id='" . $_SESSION['SESS_FIRST_NAME'] . "'";


use MySQLs SUM() function:

$query="SELECT SUM(total), * FROM clientdata WHERE ADDTime BETWEEN DATE_SUB(NOW(), INTERVAL 30 DAY) AND NOW() AND member_id='" . $_SESSION['SESS_FIRST_NAME'] . "'";

only use the , * bit of the query if you want/need more data out.


Your question isn't clear, and you didn't provide your table layout, but when you say "total of all fields in Total column", does that mean you have a table that looks like:

id | field1 | field2 | field3 | ... | Total | UserID

and you want the Total column to equal field1 + field2 + field3 + ...? Or do you have some thing like:

id | Type     | Value | UserID
1  | 'field1' |  ...  |  ..
2  | 'field2' |  ...  |  ..
3  | 'field3' |  ...  |  ..
4  | 'Total'  |  ...  |  ..

For the first one, the query is simple:

SELECT UserID, SUM(field1 + field2 + field3 + ...) AS Total
FROM table
GROUP BY UserID

for the second:

SELECT UserID, SUM(Value) AS Total
FROM table
GROUP BY 'UserID', 'Type'
WHERE ('Field' <> 'Total')

... or maybe I'm totally misreading your question.

0

精彩评论

暂无评论...
验证码 换一张
取 消