Jump to content

Trying to JOIN two date columns with WHERE conditions


NiallFH
Go to solution Solved by Barand,

Recommended Posts

Good afternoon all, I'm looking for some help with joins and dates.  Not something I have ever done before so hoping that someone can point me in the right direction.  Here is the code I have just now which works absolutely fine in terms of find the person with the oldest date of birth:

$get_oldest_player = mysql_query("
SELECT 
	P.PlayerDOB AS dob,
	CONCAT(P.PlayerFirstName, ' ', P.PlayerLastName) AS name, 
	P.PlayerID AS id
FROM 
	tplss_players P, tplss_appearances A, tplss_matches M
WHERE 
	P.PlayerID = A.AppearancePlayerID AND
	A.AppearanceMatchID = M.MatchID AND
	M.MatchDateTime = '$matchdate'
ORDER BY
	dob ASC
LIMIT 0,1
",$connection) or die(mysql_error());

Problem is, I have two tables I'm wanting to include in this query, and not entirely sure how I combine them in the search.  I have ONE table for the people (tplss_players) but TWO tables recorded instances of their involvement in matches (tplss_appearances and tplss_substitutions). 

 

Should I use a LEFT JOIN somehow ?  Not sure where it sits in the layout of the query?

 

tplss_appearances and tplss_substitutions are the same in their structure.  

 

Any help would be greatly appreciated.

 

Niall

Link to comment
Share on other sites

  • Solution

You appear to have a structure like this

+--------------+                                         +--------------+
|  player      |                                         |    match     |
+--------------+                                         +--------------+
        |                                                      |
     playerid                                               matchid
        |                  +-------------------+               |
        +-----------------<|   appearances     |>--------------+
        |                  +-------------------+               |
        |                                                      |
        |                                                      |
        |                  +-------------------+               |
        +-----------------<|  substitutions    |>--------------+
                           +-------------------+

so you going to need all the players (from appearances and substitutions) that were involved in the match. This will require a UNION and not a JOIIN

(You could consider combining these table into one table with an extra column denoting Appearance or Substitution)

SELECT 
    P.PlayerDOB AS dob,
    CONCAT(P.PlayerFirstName, ' ', P.PlayerLastName) AS name, 
    P.PlayerID AS id
FROM 
    tplss_players P
    INNER JOIN
        (
            SELECT AppearancePlayerID as PlayerID
            , AppearanceMatchID as MatchID
            FROM tplss_appearances
            UNION
            SELECT SubstitutionPlayerID as PlayerID
            , SubstitutionMatchID as MatchID
            FROM tplss_substitutions 
        ) 
        as total USING (PlayerID)
    INNER JOIN tplss_matches M USING (MatchID)
WHERE 
    M.MatchDateTime = '$matchdate'
ORDER BY
    dob ASC
LIMIT 0,1
Edited by Barand
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.