i am using mysql. i have 3 tables with following structures. these are not actual tables i am working on.
table A
id varchar(16),name varchar(255),InDate datetime
table B
id varchar(16),name varchar(255),InDate datetime
table C
id varchar(16),name varchar(255),InDate datetime
what i want to do is obtain no of rows from each table that satisfies the following condition.
in the webpage user selects two dates. i have to obtain count from each table(A,B,C) where InDate in each table falls inbetween the user selected dates. i.e if user selects march 1st and march 30 then, from table A i have to obtain no.of rows where InDate falls in between march 1st and march 30. similarly from each table.
the query should result something like this
mar1st mar2nd mar3rd.................. mar30
A 10 5 NULL
B 2 8 7
C 5 3 9
assuming that A contains 10 rows where InDate is mar1,5 rows where InDate is mar2nd. no rows for mar3.
i am trying with following queries. i am using php.
Expand|Select|Wrap|Line Numbers
- $query=
- select
- (select count(*) from A where date(InDate) between date('2009-03-01 00:01:01') and date('2009-03-29 00:01:01')) as A-count,
- (select count(*) from B where date(InDate) between date('2009-03-01 00:01:01') and date('2009-03-29 00:01:01')) as B-count,
- (select count(*) from C where date(InDate) between date('2009-03-01 00:01:01') and date('2009-03-29 00:01:01'))as C-count;
Expand|Select|Wrap|Line Numbers
- $query = NULL;
- $tempquery = NULL;
- $dt=25;
- $month = 2;
- $year = 2009;
- $flag = true;
- $enddate = '2009-3-30';
- while($flag === true){
- if(checkdate($month,$dt,$year)){
- $date = $year.'-'.$month.'-'.$dt;
- }else if(checkdate($month+1,1,$year)){
- $month = $month+1;
- $dt = 1;
- continue;
- }else if(checkdate($month,1,$year+1)){
- $year = $year+1;
- $dt = 1;
- continue;
- }
- if(strtotime($date)>strtotime($enddate)){
- $flag = false;
- break;
- }
- $datestr = date('MjS',strtotime($date));
- if($tempquery == NULL){
- $tempquery = "(SELECT COUNT(*) FROM A WHERE DATE(InDate) BETWEEN DATE('".$date." 00:01:01')
- AND DATE('".$date." 23:59:59')) AS A_".$datestr;
- }else{
- $tempquery .= ",(SELECT COUNT(*) FROM A WHERE DATE(InDate) BETWEEN DATE('".$date." 00:01:01')
- AND DATE('".$date." 23:59:59')) AS A_".$datestr;
- }
- $dt++;
- }
- $query = "SELECT ".$tempquery;
otherwise if there is any better way to implement this, pls suggest that
thanks