English 中文(简体)
计算总数
原标题:Calculate Total in a Given Row

我正在开发一个需要这样做的制度:

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

请让我问: 查询单例为30天

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

因此,你还可以在本届会议上增加以下员额的守则细节。

问题回答

我不知道我是否正确,但我认为你希望这样做。

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

利用MySQLs SUM(功能:

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

仅使用<代码>,* 查询范围,如果你想要/要求提供更多数据。

你的提问是明确的,你没有提供你的桌子布,但当你说“一栏中所有领域的总和”时,你就会有一个这样的表格:

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

并且希望<代码>Total 栏到平等field1 + Field2 + field3 + ? 或者说,你们有一些东西:

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

第一个问题是:

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

第二部分:

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

......或也许我会完全误解你的问题。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签