One left join works, VERY similar one doesn't

Status
Not open for further replies.

bubbles

Domainers...
Apr 11, 2007
858
27
0
Here is my table structure:
Table: User
Fields: UserId,FirstName,LastName,FriendCount

Table: Network_User
Fields: UserId,NetworkId

Table: Network
Fields: NetworkId, Network

Table: Keyword_User
Fields: UserId,KeywordId

Table: Keyword
Fields: KeywordId, Keyword

This query works correctly:

SELECT u.UserId,u.FirstName,u.LastName,u.FriendCount FROM `User` u
LEFT JOIN `Network_User` nu ON nu.UserId = u.UserId
LEFT JOIN `Network` n ON nu.NetworkId = n.NetworkId
WHERE n.Network = 'Purdue' ORDER BY u.FriendCount DESC
It returns 2 rows.

This one does not:
SELECT u.UserId,u.FirstName,u.LastName,u.FriendCount FROM `User` u
LEFT JOIN `Keyword_User` ku ON ku.UserId = u.UserId
LEFT JOIN `Keyword` k ON ku.KeywordId = k.KeywordId
WHERE k.Keyword = 'weeds' ORDER BY u.FriendCount DESC
It returns an empty set, where it should return 2 rows like the other one.

I know the correct data is in the tables for it to return.

I noticed that
SELECT * FROM `Keyword` WHERE `Keyword` = 'weeds'
Also fails.
While
SELECT * FROM `Network` WHERE `Network` = 'Purdue'
Works fine
Any idea why?
 


Fixed.

Correct queries were
SELECT u.UserId,u.FirstName,u.LastName,u.FriendCount FROM `User` u
LEFT JOIN `Network_User` nu ON nu.UserId = u.UserId
LEFT JOIN `Network` n ON ( nu.NetworkId = n.NetworkId AND n.Network = 'Purdue' )
ORDER BY u.FriendCount DESC

SELECT u.UserId,u.FirstName,u.LastName,u.FriendCount FROM `User` u
LEFT JOIN `Keyword_User` ku ON ku.UserId = u.UserId
LEFT JOIN `Keyword` k ON ( ku.KeywordId = k.KeywordId AND k.Keyword = 'weeds' )
ORDER BY u.FriendCount DESC
Had to specify my where column in the joins as well.
 
Status
Not open for further replies.