Jump to content

Using query from one table to query another table on same page?


johnny

Recommended Posts

Ok I am beating my head against the wall trying to do this.  Here are the tables I am working with:

tablename = schedule 
__________________
week  |  team  |  name
1      |    tm1  |  A
1      |    tm2  |  B
2      |  tm1  |  C
2      |    tm2  |  B

tablename = points
___________________
name    |  1    |  2 
A        |  4    |  -2
B        |  0    |  3
C        |  2    |  1


so I have a page where I want it to able to display the teams that play eachother (week1.tm1. vs week2.tms) as well as the player on each team AND how many pts they scored.  so far I can get the teams and the players pretty easily, because they are in the same table. 

what i cannot figure out is how to code it to find that in Week 1, Team 1 had player A...then take that to the points page and pull the points that A scored in Week 1.  I am currently trying this:

[code]<?php

$link = mysql_connect("xxx", "xxx", "xxx")
  or die("Could not connect : " . mysql_error());
mysql_select_db("xxx") or die("Could not select database");
$query="
SELECT points.1
FROM points.schedule
WHERE points.name= schedule.name
AND schedule.week = '1'
AND schedule.team = 'tm1'
";
$res = mysql_query($query);
while($row = mysql_fetch_assoc($res))
{
echo $row ['1'].'<br>';
}



?>[/code]

I have also tried something like:
[code]
"SELECT 1
FROM points
WHERE name = (SELECT name FROM schedule WHERE week='1' AND team='tm1')
"[/code]

Both give me "Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in line xx"

I'll buy someone a keg to figure this out lol.
Link to comment
Share on other sites

Probably because the column names are not legal! Try changing them to 'one' and 'two', it's the only reason I can see why your query produces an error.

When you've done that, you can do

"select week, team, schedule.name, one, two from schedule, points where schedule.name=points.name"

then you will get the all the teams with all the points scored by the player in the team for week one and week two, week by week.
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.