Skip to main content
December 24, 2009
Answered

MySQL temp tables or Calling Stored procedures in CS4

  • December 24, 2009
  • 2 replies
  • 1098 views

I need to filter a result set by username before performing a LEFT JOIN and including OR IS NULL rows. The SQL works from from the mysqli client, by either creating a temp table using  "create temporary table temp_appts select * from..."  Or by creating a stored procedure which contains the same "create temp table" statement, then running my LEFT JOIN statement against the temp table.

I have tried both in CS4, it accepts the code in "code view" without errors but the page in a browser loads as a blank or a 500 error in IE. In my experience, this is caused by exiting before reaching the html header.

Is it possible to do either in CS4?  Below is the code I am using.

$varU_temp_appts = "-1";
if (isset(<?php $_SESSION['MM_Username'])){
$varU_temp_appts =$_SESSION['MM_Username'];
}

mysql_select_db($database_ess, $ess);
$query_temp_appts = sprintf("CREATE TEMPORARY TABLE temp_appts SELECT * FROM appts WHERE username=%s", /GetSQLValueString($varU_temp_appts, "text"));
$temp_appts = mysql_query($query_temp_appts, $ess) or die(mysql_error());
$row_temp_appts = mysql_fetch_assoc($temp_appts);
$totalRows_temp_appts = mysql_num_rows($temp_appts);


mysql_select_db($database_ess, $ess);
$query_todays_appts = "SELECT *  FROM appt_tm LEFT JOIN (temp_appts) ON appt_tm.appt_time=temp_appts.appt_hr WHERE(appt_date=CURDATE() OR appt_date IS NULL)";
$todays_appts = mysql_query($query_todays_appts, $ess) or die(mysql_error());
$row_todays_appts = mysql_fetch_assoc($todays_appts);
$totalRows_todays_appts = mysql_num_rows($todays_appts);

Any help is appreciated!

This topic has been closed for replies.
Correct answer bregent

Why not just use a derived table rather than creating a temp table?

2 replies

December 26, 2009

By creating a table of all possible appt dates and adding a CROSS JOIN before my LEFT JOIN, I get the desired result. Not crazy about maintaining a table of dates, but i'm sure an event can be written to generate new dates and prune old ones.

Thanks again for your help BREGENT and David.

bregentCorrect answer
Participating Frequently
December 25, 2009

Why not just use a derived table rather than creating a temp table?

December 25, 2009

I've not done that before, I'll look it up.

Thanks!

December 25, 2009

Seems to not be happy once I add the LEFT JOIN, I can break it down into smaller pieces and it works as a FROM clause but adding the join breaks it.

SELECT *  FROM (SELECT * FROM appts WHERE username=%s)appt_tm LEFT JOIN (appts) ON appt_tm.appt_time=appts.appt_hr WHERE appt_date=CURDATE()OR appt_date IS NULL;

I get   "ERROR 1052 (2300): Column 'appt_date' in where clause is ambiguous", I found there was a bug in MySQL 4. that caused this but I'm running 5.

Thanks again