PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > mysql.general > Left join is not doing what I thought it should do.
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Left join is not doing what I thought it should do.

Réponse
 
LinkBack Outils de la discussion
Vieux 22/08/2007, 17h23   #1
Critters
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Left join is not doing what I thought it should do.

I have 3 tables
A users table (userID, userName)
A leaderboard table (userID, score)
A friends table (userIDA, userIDB)

I would like to produce the following result:

userName, score, userIDA
Dave, 100, 1
Simon, 200, 5
Paul, 300, NULL

The 3rd record is NULL as there is no record in "friends" with a userIDB
matching users (or leaderboard) userID

I have tried this:

SELECT users.username, gameLeaderboards.playerpoints, friends.userA
FROM gameLeaderboards
JOIN users ON gameLeaderboards.userID = users.ID
LEFT JOIN friends ON gameLeaderboards.userID = friends.userB
WHERE friends.userA = 79760

The where is so there is only a value in the "userIDA" column if the
user is friends with userID 79760

But what I get instead of lots of records with 79760 and NULLs is just
records from the leaderboard table that have a matching userID in the
friend table, hmmf

Any ideas?

Thanks
-
Dave

  Réponse avec citation
Vieux 22/08/2007, 19h30   #2
Jerry Schwartz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut RE: Left join is not doing what I thought it should do.

I think your problem is that you can't have a "missing" friends record that
also has a non-null value for friends.userA. If friends.userA = 79760, then
you've found a record.

You can have records where userA is something valid and UserB is null, but
then you can't join on UserB.

Does that ?

Regards,

Jerry Schwartz
The Infoshop by Global Information Incorporated
195 Farmington Ave.
Farmington, CT 06032

860.674.8796 / FAX: 860.674.8341

www.the-infoshop.com
www.giiexpress.com
www.etudes-marche.com


> -----Original Message-----
> From: Critters [mailto:critters@desktopcreatures.com]
> Sent: Wednesday, August 22, 2007 12:23 PM
> To: MySQL General
> Subject: Left join is not doing what I thought it should do.
>
> I have 3 tables
> A users table (userID, userName)
> A leaderboard table (userID, score)
> A friends table (userIDA, userIDB)
>
> I would like to produce the following result:
>
> userName, score, userIDA
> Dave, 100, 1
> Simon, 200, 5
> Paul, 300, NULL
>
> The 3rd record is NULL as there is no record in "friends"
> with a userIDB
> matching users (or leaderboard) userID
>
> I have tried this:
>
> SELECT users.username, gameLeaderboards.playerpoints, friends.userA
> FROM gameLeaderboards
> JOIN users ON gameLeaderboards.userID = users.ID
> LEFT JOIN friends ON gameLeaderboards.userID = friends.userB
> WHERE friends.userA = 79760
>
> The where is so there is only a value in the "userIDA" column if the
> user is friends with userID 79760
>
> But what I get instead of lots of records with 79760 and
> NULLs is just
> records from the leaderboard table that have a matching userID in the
> friend table, hmmf
>
> Any ideas?
>
> Thanks
> -
> Dave
>
>
> --
> MySQL General Mailing List
> For list archives: http://lists.mysql.com/mysql
> To unsubscribe:
> http://lists.mysql.com/mysql?unsub=j...e-infoshop.com
>
>




  Réponse avec citation
Vieux 22/08/2007, 20h30   #3
Critters
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Left join is not doing what I thought it should do.

It works if I do AND instead of WHERE
Go figure

LEFT JOIN friends ON gameLeaderboards.userID = friends.userB
AND friends.userA = 79760

--
Dave

Jerry Schwartz wrote:
> I think your problem is that you can't have a "missing" friends record that
> also has a non-null value for friends.userA. If friends.userA = 79760, then
> you've found a record.
>
> You can have records where userA is something valid and UserB is null, but
> then you can't join on UserB.
>
> Does that ?
>
> Regards,
>
> Jerry Schwartz
> The Infoshop by Global Information Incorporated
> 195 Farmington Ave.
> Farmington, CT 06032
>
> 860.674.8796 / FAX: 860.674.8341
>
> www.the-infoshop.com
> www.giiexpress.com
> www.etudes-marche.com
>
>
>
>> -----Original Message-----
>> From: Critters [mailto:critters@desktopcreatures.com]
>> Sent: Wednesday, August 22, 2007 12:23 PM
>> To: MySQL General
>> Subject: Left join is not doing what I thought it should do.
>>
>> I have 3 tables
>> A users table (userID, userName)
>> A leaderboard table (userID, score)
>> A friends table (userIDA, userIDB)
>>
>> I would like to produce the following result:
>>
>> userName, score, userIDA
>> Dave, 100, 1
>> Simon, 200, 5
>> Paul, 300, NULL
>>
>> The 3rd record is NULL as there is no record in "friends"
>> with a userIDB
>> matching users (or leaderboard) userID
>>
>> I have tried this:
>>
>> SELECT users.username, gameLeaderboards.playerpoints, friends.userA
>> FROM gameLeaderboards
>> JOIN users ON gameLeaderboards.userID = users.ID
>> LEFT JOIN friends ON gameLeaderboards.userID = friends.userB
>> WHERE friends.userA = 79760
>>
>> The where is so there is only a value in the "userIDA" column if the
>> user is friends with userID 79760
>>
>> But what I get instead of lots of records with 79760 and
>> NULLs is just
>> records from the leaderboard table that have a matching userID in the
>> friend table, hmmf
>>
>> Any ideas?
>>
>> Thanks
>> -
>> Dave
>>
>>
>> --
>> MySQL General Mailing List
>> For list archives: http://lists.mysql.com/mysql
>> To unsubscribe:
>> http://lists.mysql.com/mysql?unsub=j...e-infoshop.com
>>
>>
>>

>
>
>
>
>


  Réponse avec citation
Vieux 22/08/2007, 20h58   #4
Jerry Schwartz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut RE: Left join is not doing what I thought it should do.

By using AND, you've moved the test for 79760 into the JOIN condition. I
wouldn't have thought of that, either. This seems to be a peculiarity of a
LEFT JOIN. Normally, if you compare a NULL value against anything, even
another NULL, the result is NULL (neither true nor false). I don't
understand what that would do to an AND operation, I would think the result
would be false.

I hope someone else can explain that to us.

Regards,

Jerry Schwartz
The Infoshop by Global Information Incorporated
195 Farmington Ave.
Farmington, CT 06032

860.674.8796 / FAX: 860.674.8341

www.the-infoshop.com
www.giiexpress.com
www.etudes-marche.com


> -----Original Message-----
> From: Critters [mailto:critters@desktopcreatures.com]
> Sent: Wednesday, August 22, 2007 3:30 PM
> To: Jerry Schwartz
> Cc: 'MySQL General'
> Subject: Re: Left join is not doing what I thought it should do.
>
> It works if I do AND instead of WHERE
> Go figure
>
> LEFT JOIN friends ON gameLeaderboards.userID = friends.userB
> AND friends.userA = 79760
>
> --
> Dave
>
> Jerry Schwartz wrote:
> > I think your problem is that you can't have a "missing"

> friends record that
> > also has a non-null value for friends.userA. If

> friends.userA = 79760, then
> > you've found a record.
> >
> > You can have records where userA is something valid and

> UserB is null, but
> > then you can't join on UserB.
> >
> > Does that ?
> >
> > Regards,
> >
> > Jerry Schwartz
> > The Infoshop by Global Information Incorporated
> > 195 Farmington Ave.
> > Farmington, CT 06032
> >
> > 860.674.8796 / FAX: 860.674.8341
> >
> > www.the-infoshop.com
> > www.giiexpress.com
> > www.etudes-marche.com
> >
> >
> >
> >> -----Original Message-----
> >> From: Critters [mailto:critters@desktopcreatures.com]
> >> Sent: Wednesday, August 22, 2007 12:23 PM
> >> To: MySQL General
> >> Subject: Left join is not doing what I thought it should do.
> >>
> >> I have 3 tables
> >> A users table (userID, userName)
> >> A leaderboard table (userID, score)
> >> A friends table (userIDA, userIDB)
> >>
> >> I would like to produce the following result:
> >>
> >> userName, score, userIDA
> >> Dave, 100, 1
> >> Simon, 200, 5
> >> Paul, 300, NULL
> >>
> >> The 3rd record is NULL as there is no record in "friends"
> >> with a userIDB
> >> matching users (or leaderboard) userID
> >>
> >> I have tried this:
> >>
> >> SELECT users.username, gameLeaderboards.playerpoints, friends.userA
> >> FROM gameLeaderboards
> >> JOIN users ON gameLeaderboards.userID = users.ID
> >> LEFT JOIN friends ON gameLeaderboards.userID = friends.userB
> >> WHERE friends.userA = 79760
> >>
> >> The where is so there is only a value in the "userIDA"

> column if the
> >> user is friends with userID 79760
> >>
> >> But what I get instead of lots of records with 79760 and
> >> NULLs is just
> >> records from the leaderboard table that have a matching

> userID in the
> >> friend table, hmmf
> >>
> >> Any ideas?
> >>
> >> Thanks
> >> -
> >> Dave
> >>
> >>
> >> --
> >> MySQL General Mailing List
> >> For list archives: http://lists.mysql.com/mysql
> >> To unsubscribe:
> >> http://lists.mysql.com/mysql?unsub=j...e-infoshop.com
> >>
> >>
> >>

> >
> >
> >
> >
> >

>
>




  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 23h13.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14710 seconds with 12 queries