473,385 Members | 1,973 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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),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
  1. $query=
  2. select
  3. (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,
  4. (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,
  5. (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;
  6.  
the above query is giving total count. but not datewise.
Expand|Select|Wrap|Line Numbers
  1.  
  2.                 $query = NULL;
  3.                 $tempquery = NULL;
  4.  
  5.                 $dt=25;
  6.                 $month = 2;
  7.                 $year = 2009;
  8.                 $flag = true;
  9.                 $enddate = '2009-3-30';
  10.  
  11.                 while($flag === true){
  12.                                 if(checkdate($month,$dt,$year)){
  13.                                                 $date = $year.'-'.$month.'-'.$dt;
  14.                                 }else if(checkdate($month+1,1,$year)){
  15.                                                 $month = $month+1;
  16.                                                 $dt = 1;
  17.                                                 continue;
  18.                                 }else if(checkdate($month,1,$year+1)){
  19.                                                 $year = $year+1;
  20.                                                 $dt = 1;
  21.                                                 continue;
  22.                                 }
  23.                                 if(strtotime($date)>strtotime($enddate)){
  24.                                                 $flag = false;
  25.                                                 break;
  26.                                 }
  27.                                 $datestr = date('MjS',strtotime($date));
  28.                                 if($tempquery == NULL){
  29.                                                 $tempquery = "(SELECT COUNT(*) FROM A  WHERE DATE(InDate) BETWEEN DATE('".$date." 00:01:01')
  30.                                                 AND DATE('".$date." 23:59:59')) AS  A_".$datestr;
  31.                                 }else{
  32.                                                 $tempquery .= ",(SELECT COUNT(*) FROM A  WHERE DATE(InDate) BETWEEN DATE('".$date." 00:01:01')
  33.                                                 AND DATE('".$date." 23:59:59')) AS A_".$datestr;                        
  34.                                 }
  35.                                 $dt++;
  36.                 }
  37.  
  38.                 $query = "SELECT ".$tempquery;
  39.  
  40.  
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
Mar 30 '09 #1
1 3781
code green
1,726 Expert 1GB
You do not need the DATE() function call.
Expand|Select|Wrap|Line Numbers
  1. select count(*) from A where InDate 
  2. between '2009-03-01 00:01:01' and '2009-03-29 00:01:01' 
  3. as A-count
is all that is needed.
But I am not sure what difference it will make.
Apr 6 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: knoak | last post by:
Hi there, I have a small question: I have a table with lots of rows in it. Of course all have a different id, but each can be assigned to a certain category. Categories correspond with 1 - 10....
18
by: jimfortune | last post by:
I have an A97 module called modWorkdayFunctions in: http://www.oakland.edu/~fortune/WorkdayFunctions.zip It allows the counting of workdays taking into consideration up to 11 U.S. holidays. ...
0
by: Ireneus Broncel | last post by:
I have a class which reads Groups and Users from ActiveDirectory. The Problem is, that i have about 10000 rows as product. When I am trying to read the "memberOf" Objects out of this field i get...
2
by: Alpha | last post by:
I have a window application. In one of the form, a datagrid has a dataview as its datasource. Initial filtering result would give the datavew 3 items. When I double click on the datagrid to edit...
3
by: Hyphessobricon | last post by:
Hallo, Indeed, a count of a query with a group by function gives more records than there are and so for-next structures don't function. How is this to be mended. Anyone? Everyone in fact....
4
by: Peter W Johnson | last post by:
Hi guys, I have a problem with a datagrid record count. Here is the code:- <snip> Public Class frmMerchantDeposit Inherits System.Windows.Forms.Form Dim myconnection As New...
4
by: Chris | last post by:
Can't seem to figure out how to do this and have been reading for some time now...... I want to select a row count from a table name in SYSTABLES. This statement does not return what I needed,...
2
by: leegold58 | last post by:
Newbie question must of been asked before but: How do I count the number of tables in a database? Without VB, prefer to use SQL or the Access GUI somehow. Thanks. Lee g.
1
by: asrir | last post by:
If there are no records returned by the sql what should be the value of myDataSet.Tables.Count. Is it 0 or 1 (with rows count 0)? The same application returns 0 on one machine and 1 on another. We...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.