English 中文(简体)
MySQL:SQL在一份发言中列举若干条件的总和
原标题:MySQL: list of sum for several conditions in one SQL statement
  • 时间:2010-07-14 11:16:43
  •  标签:
  • mysql

我对我的我的我的我的我的卡勒发言有问题:

select sum(x) as "sum", "01.06.2010" as "date" from (
   select distinct a.id as ID, a.dur as x from b_c 
      inner join c on b_c.c_id = c.id 
      inner join a on c.id = a.c_id 
      inner join a_au on a.id = a_aud.id 
      inner join d on a_au.rev = d.rev 
      where 
         b_c.b_id = 30 and 
         a_au.stat != 1 and 
         DATE(FROM_UNIXTIME(rtime)) = DATE( 2010-06-01 )
) AS SubSelectTable;

这份声明一行“摘要”和“日期”。

现在我不仅有一个日期(2010-06-01),而且我有许多日期要拿到这笔钱,而不是一个单行。 我希望每天有一次结果。

因此,例如,上述发言使我返回。

sum            date
-------------------
 20      01.06.2010

Now I have the dates 2010-06-01, 2010-06-02, 2010-06-03, ... What I can do is to write for every date one sql statement changing the DATE. So if I have 10 dates, I will have 10 sql statements for the database. But I did want it with one SQL statement, and the result should not be the sum of all given dates, I want to have one result row per date. So the result should like that:

sum            date
-------------------
 20      01.06.2010
133      02.06.2010
 19      03.06.2010
 88      04.06.2010
...             ...

我如何能够这样做? 是否有任何人知道?

Thanks advance lot

最佳回答

按指挥使用我的小组。 完全没有经过测试的法典实例:

select sum(x) as "sum", date as "date" from (
   select distinct a.id as ID, a.dur as x, DATE(FROM_UNIXTIME(rtime)) as date from b_c 
      inner join c on b_c.c_id = c.id 
      inner join a on c.id = a.c_id 
      inner join a_au on a.id = a_aud.id 
      inner join d on a_au.rev = d.rev 
      where 
         b_c.b_id = 30 and 
         a_au.stat != 1
) AS SubSelectTable
group by date;

See: http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html

问题回答

暂无回答




相关问题
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 ...

热门标签