kdalts
01-17-2012, 10:20 AM
Guys, not sure if this should be in the PHP or MySQL thread...
I have a large database (over 60,000 records) that will grow about 5,000 records per month
I need to pull records (mainly record counts) based on a selected Month
I also need to pull records for trending based on a selected month less 1,2,3 or whatever the trend period is
So here is my challenge...
Currently my code uses 3 SELECT WHERE clauses just to find records for a 3 month period...
//FETCH THE TOTAL FSO'S
//Current selected month ($mth_1)
$all_FSOs_sql = "SELECT REGION FROM dispatches WHERE Month = '" . $mth_1 . "' AND Region = '" . $region . "'" ;
$result = mysql_query($all_FSOs_sql, $conn) or trigger_error("SQL", E_USER_ERROR);
$all_FSOs_sql_mth1 = mysql_num_rows($result);
//Current selected month less 1 month ($mth_2)
$all_FSOs_sql = "SELECT REGION FROM dispatches WHERE Month = '" . $mth_2 . "' AND Region = '" . $region . "'" ;
$result = mysql_query($all_FSOs_sql, $conn) or trigger_error("SQL", E_USER_ERROR);
$all_FSOs_sql_mth2 = mysql_num_rows($result);
//Current selected month less 1 month ($mth_3)
$all_FSOs_sql = "SELECT REGION FROM dispatches WHERE Month = '" . $mth_3 . "' AND Region = '" . $region . "'" ;
$result = mysql_query($all_FSOs_sql, $conn) or trigger_error("SQL", E_USER_ERROR);
$all_FSOs_sql_mth3 = mysql_num_rows($result);
This works fine... however, I then want to count the number of records for several partners for each of those given months. I have 10 partners. So I then end up doing 10 more SELECT WHERE clauses (1 for each partner)...
/FETCH FSO' BY PARTNER
$partner1_sql = "SELECT REGION FROM dispatches WHERE Dispatched_TPM = '" . $partner1 . "' AND Month = '" . $selected_mth . "'" ;
$result = mysql_query($partner1_sql, $conn) or trigger_error("SQL", E_USER_ERROR);/**** end deal with the database ****/
$partner1_num_rows = mysql_num_rows($result);
$partner2_sql = "SELECT REGION FROM dispatches WHERE Dispatched_TPM = '" . $partner2 . "' AND Month = '" . $selected_mth . "'" ;
$result = mysql_query($partner2_sql, $conn) or trigger_error("SQL", E_USER_ERROR);/**** end deal with the database ****/
$partner2_num_rows = mysql_num_rows($result);
etc... for all other partners
This all works but seems like a lot of coding and I know I must be doing it wrong.
Basically, I figure there must be a way to pull the numcount for each partner based on a single SELECT clause for each month but I can't figure it out...any help would be appreciated please
I have a large database (over 60,000 records) that will grow about 5,000 records per month
I need to pull records (mainly record counts) based on a selected Month
I also need to pull records for trending based on a selected month less 1,2,3 or whatever the trend period is
So here is my challenge...
Currently my code uses 3 SELECT WHERE clauses just to find records for a 3 month period...
//FETCH THE TOTAL FSO'S
//Current selected month ($mth_1)
$all_FSOs_sql = "SELECT REGION FROM dispatches WHERE Month = '" . $mth_1 . "' AND Region = '" . $region . "'" ;
$result = mysql_query($all_FSOs_sql, $conn) or trigger_error("SQL", E_USER_ERROR);
$all_FSOs_sql_mth1 = mysql_num_rows($result);
//Current selected month less 1 month ($mth_2)
$all_FSOs_sql = "SELECT REGION FROM dispatches WHERE Month = '" . $mth_2 . "' AND Region = '" . $region . "'" ;
$result = mysql_query($all_FSOs_sql, $conn) or trigger_error("SQL", E_USER_ERROR);
$all_FSOs_sql_mth2 = mysql_num_rows($result);
//Current selected month less 1 month ($mth_3)
$all_FSOs_sql = "SELECT REGION FROM dispatches WHERE Month = '" . $mth_3 . "' AND Region = '" . $region . "'" ;
$result = mysql_query($all_FSOs_sql, $conn) or trigger_error("SQL", E_USER_ERROR);
$all_FSOs_sql_mth3 = mysql_num_rows($result);
This works fine... however, I then want to count the number of records for several partners for each of those given months. I have 10 partners. So I then end up doing 10 more SELECT WHERE clauses (1 for each partner)...
/FETCH FSO' BY PARTNER
$partner1_sql = "SELECT REGION FROM dispatches WHERE Dispatched_TPM = '" . $partner1 . "' AND Month = '" . $selected_mth . "'" ;
$result = mysql_query($partner1_sql, $conn) or trigger_error("SQL", E_USER_ERROR);/**** end deal with the database ****/
$partner1_num_rows = mysql_num_rows($result);
$partner2_sql = "SELECT REGION FROM dispatches WHERE Dispatched_TPM = '" . $partner2 . "' AND Month = '" . $selected_mth . "'" ;
$result = mysql_query($partner2_sql, $conn) or trigger_error("SQL", E_USER_ERROR);/**** end deal with the database ****/
$partner2_num_rows = mysql_num_rows($result);
etc... for all other partners
This all works but seems like a lot of coding and I know I must be doing it wrong.
Basically, I figure there must be a way to pull the numcount for each partner based on a single SELECT clause for each month but I can't figure it out...any help would be appreciated please