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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.