Jump to content

Sub query problem


ninedoors

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/120188-sub-query-problem/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/120188-sub-query-problem/#findComment-619183
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.