Jump to content

INNER JOIN, LEFT JOIN, RIGHT JOIN??? HELP!


Recommended Posts

Hi

Just a summary about my tables:

[b]players_answers[/b]
- id (auto_inc)
- date
- player_id
- value

[b]players[/b]
- id (auto_inc)
- player_id
- givenname
- surname

Here is my query:

[code]
SELECT players_answers.player_id, players_answers.date, players.player_id, players.givenname,  players.surname
FROM players_answers
INNER JOIN players
ON (players_answers.player_id = players.player_id AND date=NOW())
GROUP BY players_answers.player_id
[/code]

What i'm trying to do is grab all the players names that have an entry on today's date. [!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]Which works fine![!--colorc--][/span][!--/colorc--]

[b]My problem is[/b] that how would I go about getting all the players name's that don't have an entry on this date. So the exact opposite of this query!

I have tried to use date!=NOW(). But the problem is all players in the players_answers have dates ranging back to Janurary, so all players get displayed when I put that clause in.

[b]Please someone help![/b]

Thank You!
Link to comment
https://forums.phpfreaks.com/topic/9467-inner-join-left-join-right-join-help/
Share on other sites

Try the following (UNTESTED):

[code]SELECT players_answers.player_id, players_answers.date, players.player_id, players.givenname,  players.surname
FROM players
LEFT JOIN players_answers ON (players_answers.player_id = players.player_id AND date=NOW())
WHERE players_answers.player_id IS NULL[/code]

BTW, you shouldn't have the group by in either of these.
[!--quoteo(post=372954:date=May 10 2006, 10:59 AM:name=fenway)--][div class=\'quotetop\']QUOTE(fenway @ May 10 2006, 10:59 AM) [snapback]372954[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Try the following (UNTESTED):

[code]SELECT players_answers.player_id, players_answers.date, players.player_id, players.givenname,  players.surname
FROM players
LEFT JOIN players_answers ON (players_answers.player_id = players.player_id AND date=NOW())
WHERE players_answers.player_id IS NULL[/code]

BTW, you shouldn't have the group by in either of these.
[/quote]


Hi Fenway!

The following query displays every player_id. :(

I have asked every person I know that knows mySql about this and no one can work it out!

[quote]
[quote]

Try the following (UNTESTED):

[code]
SELECT players_answers.player_id, players_answers.date, players.player_id, players.givenname,  players.surname
FROM players
LEFT JOIN players_answers ON (players_answers.player_id = players.player_id AND date=NOW())
WHERE players_answers.player_id IS NULL
[/code]

BTW, you shouldn't have the group by in either of these.
[/quote]


Hi Fenway!

The following query displays every player_id. :(

I have asked every person I know that knows mySql about this and no one can work it out!
[/quote]
From the way you've described your tables, as far as I can see fenway's query should have worked. I have tested it on sample data and get the desired results. Perhaps you should post the script you're currently using to retrieve the players. You may have forgotten something.


The following should also work, but I'd assume the JOIN would be faster.
[code]
SELECT
player_id, givenname, surname
FROM
players
WHERE player_id
NOT IN
    (
    SELECT
    players.player_id
    FROM players_answers
    INNER JOIN players
    ON (players_answers.player_id = players.player_id AND date=NOW())
    )
[/code]
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.