Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8585133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:59:06+00:00 2026-06-11T21:59:06+00:00

This is my first post, but I’ve been reading here for ages taking in

  • 0

This is my first post, but I’ve been reading here for ages taking in a lot of great information.

Please bear with me as I try to explain my predicament. I wasn’t even sure how to label my question, since I don’t really know the terminology for what I’m trying to do. I’m a beginner of PHP/MySQL (this will become painfully obvious when you see my question), but I’m eager to learn.

Anyway, here goes:

I have two tables, named Games and Teams.

mysql> describe games;

+-----------+---------------+------+-----+---------+----------------+
| Field     | Type          | Null | Key | Default | Extra          |
+-----------+---------------+------+-----+---------+----------------+
| id        | int(11)       | NO   | PRI | NULL    | auto_increment |
| homeTeam  | int(11)       | NO   |     | NULL    |                |
| awayTeam  | int(11)       | NO   |     | NULL    |                |
| homeScore | int(11)       | NO   |     | NULL    |                |
| awayScore | int(11)       | NO   |     | NULL    |                |
| homeOdds  | decimal(10,2) | NO   |     | NULL    |                |
| drawOdds  | decimal(10,2) | NO   |     | NULL    |                |
| awayOdds  | decimal(10,2) | NO   |     | NULL    |                |
| gameDate  | date          | NO   |     | NULL    |                |
+-----------+---------------+------+-----+---------+----------------+

and

mysql> describe teams;

+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| id    | int(11)      | NO   | PRI | NULL    | auto_increment |
| team  | varchar(255) | NO   |     | NULL    |                |
+-------+--------------+------+-----+---------+----------------+

I would like the fields Games.homeTeam and Games.awayTeam to do a lookup for their values in the Teams.id and Teams.team fields. Basically Games.homeTeam should find its equivalent in Teams.id. Simple enough. However, I would like both Games.homeTeam and Games.awayTeam to look in the Teams table within the same SQL statement.

I figured maybe something like this would work:

$result = mysql_query("SELECT * FROM Games g, Teams ht, Teams at where g.homeTeam = ht.id AND g.awayTeam = at.id ORDER BY g.gameDate") or die(mysql_error());

and then to pick the rows up:

while($row = mysql_fetch_array($result)) { echo "Home Team: " . $row['ht.team'];}

That’s a no go, as I get a mysql error saying there’s no index named ht.team.

I hope it’s fairly clear what I’m trying to achieve here. How should I go about this? I assume I have to use aliases of some sort, but I can’t wrap my head around as to what the SQL statement should look like to accomplish what I want.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T21:59:07+00:00Added an answer on June 11, 2026 at 9:59 pm

    Don’t use SELECT * with join queries! Always be explicit about the columns you need in your SELECT list, and supply column aliases:

    SELECT 
      /* Only select the columns you actually need, and 
       * supply aliases to those with similar names
       */
      ht.team AS ht_team,
      ht.id AS ht_id,
      at.team AS at_team,
      at.id AS at_id
      g.homeScore,
      g.awayScore,
      g.homeOdds,
      g.drawOdds,
      g.awayOdds,
      g.gameDate
    FROM 
      Games g, 
      Teams ht, 
      Teams at 
    where g.homeTeam = ht.id AND g.awayTeam = at.id 
    ORDER BY g.gameDate
    

    In your PHP, access them via their aliases, for example:

    echo "Home Team: {$row['ht_team']}";
    

    By the way, although your method of implicit (comma-separated tables and conditions in the WHERE clause) joins will work, using explicit joining is often recommended. It is a newer syntax, and will allow easier expansion into LEFT JOIN or RIGHT JOIN when the need arises:

    SELECT 
      ht.team AS ht_team,
      ht.id AS ht_id,
      at.team AS at_team,
      at.id AS at_id
      g.homeScore,
      g.awayScore,
      g.homeOdds,
      g.drawOdds,
      g.awayOdds,
      g.gameDate
    FROM 
      /* Explicit JOIN syntax */
      Games g, 
      JOIN Teams ht ON g.homeTeam = ht.id 
      JOIN Teams at ON g.awayTeam = at.id 
    ORDER BY g.gameDate
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my first post here. I have been reading posts here since I
this is my first post here but I've been a frequent reader of various
This is my first post, thx, you've been very helpful. But I'm stuck. I
This is my first post, but I have been using this site for a
This is my first post, but I've been using the site for years. However,
this is my first post here. I'm using an existing Flash widget but would
This is my first post here so if I've been mistaken for my post
Usless Background Info Hello, all. This is my first post here, but I often
This is my first post here so please forgive any protocol errors. My question
This is my first post here, but I've using this site regularly to help

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.