how to get count from 3 different tables

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kummu4help
    New Member
    • Nov 2008
    • 14

    how to get count from 3 different tables

    hi,
    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),nam e varchar(255),In Date datetime

    table B
    id varchar(16),nam e varchar(255),In Date datetime

    table C
    id varchar(16),nam e varchar(255),In Date 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.
    Code:
    $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;
    the above query is giving total count. but not datewise.
    Code:
                    $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;
    this query is giving datewise count. but for only one table. how can i improve this to get count from remaining tables.

    otherwise if there is any better way to implement this, pls suggest that

    thanks
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    You do not need the DATE() function call.
    Code:
    select count(*) from A where InDate 
    between '2009-03-01 00:01:01' and '2009-03-29 00:01:01' 
    as A-count
    is all that is needed.
    But I am not sure what difference it will make.

    Comment

    Working...