Pi_Mastuh Posted January 28, 2011 Share Posted January 28, 2011 I am trying to run a rather complex query, and am getting the error: "Every derived table must have its own alias." I haven't done this kind of complex SQL before, could somebody tell me what I'm missing here? Thanks! $SQL2 = "SELECT * FROM shiftsIn WHERE date='$day' AND month='$month' AND year='$year' AND busID='$userBusID'"; $SQL = "SELECT * FROM ($SQL2) WHERE (startTime<='$startTime2' AND endTime>='$endTime2') OR (startTime IS NULL AND endTime IS NULL)"; $resultShifts = mysql_query($SQL,$connection) or die(mysql_error()); $numShifts = mysql_num_rows($resultShifts); Quote Link to comment https://forums.phpfreaks.com/topic/225987-complex-sql-query-help-needed/ Share on other sites More sharing options...
mikosiko Posted January 29, 2011 Share Posted January 29, 2011 the explanation for the error code is very simple.. http://dev.mysql.com/doc/refman/5.0/en/from-clause-subqueries.html however,,, is hard to understand what are you are trying to do using a sub-query when in reality that could be solved with ONE and simple query... just put the 2 WHERE together in one query string and "complexity" is gone Quote Link to comment https://forums.phpfreaks.com/topic/225987-complex-sql-query-help-needed/#findComment-1166774 Share on other sites More sharing options...
Pi_Mastuh Posted January 29, 2011 Author Share Posted January 29, 2011 I'm not sure how to get it to check all of those conditions though. I tried using parentheses inside of parenthesis and it didn't work. Quote Link to comment https://forums.phpfreaks.com/topic/225987-complex-sql-query-help-needed/#findComment-1166775 Share on other sites More sharing options...
fenway Posted February 11, 2011 Share Posted February 11, 2011 Can't see the actual query that way -- echo it with all those variables. Quote Link to comment https://forums.phpfreaks.com/topic/225987-complex-sql-query-help-needed/#findComment-1172729 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.