ninedoors Posted August 18, 2008 Share Posted August 18, 2008 I'm not quite sure what I am doing wrong but I am assuming that it has something to do with my sql syntax. Is there a different way you need to write sub queries than regular queries in php? Here is my query: <?php $sub_query_1 = "SELECT hometeam FROM schedule ORDER BY game_id LIMIT $limit, $games_per_week"; $sub_query_2 = "SELECT visitingteam FROM schedule ORDER BY game_id LIMIT $limit, $games_per_week"; $query = mysql_query("SELECT SUM(sum) as total FROM schedule_temp WHERE team_id = ('$sub_query_1') OR team_id = ('$sub_query_2')"); while($row = mysql_fetch_array($query)) { $game_num = $factor + $k; $game_sums[$game_num] = $row[0]; } ?> When I run this query I get a null set. But when I run both sub querys by themselves I get the right team_id. I tried just inserting the sub query string into the main query but still nothing worked. What am I doing wrong here? Nick Quote Link to comment https://forums.phpfreaks.com/topic/120188-sub-query-problem/ Share on other sites More sharing options...
Illusion Posted August 18, 2008 Share Posted August 18, 2008 I don't know what you want retrieve with your queries but I combined your queries like this SELECT SUM(sum) as total FROM schedule_temp WHERE team_id IN (SELECT DISTINCT hometeam FROM schedule ORDER BY game_id LIMIT '$limit','$games_per_week' UNION ALL SELECT DISTINCT visitingteam FROM schedule ORDER BY game_id LIMIT '$limit', '$games_per_week' ) It will be more easy for us to understand if you explain what you want to do exactly. May be joins can help you. Quote Link to comment https://forums.phpfreaks.com/topic/120188-sub-query-problem/#findComment-619183 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.