Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Used a JOIN, but ...

Status
Not open for further replies.

teken8

Technical User
May 30, 2006
4
US
I have two tables to be joined(?):

Code:
ballots Table        votes Table
_____________       ________________________
|  id       |       | user_id  | ballot_id  |
------------|       |----------+------------|
|  1        |       |  2       |  1         |
|  2        |       |  1       |  1         |
+-----------+       |  2       |  2         |
                    +----------+------------+

The query
Code:
SELECT Ballot.id,Vote.user_id
FROM ballots as Ballot
LEFT JOIN votes as Vote
ON Ballot.id=Vote.ballot_id
WHERE (Vote.user_id='1' OR Vote.user_id IS NULL)

this returns:
Code:
___________________________
| Ballot.id | Vote.user_id |
------------+--------------|
|  1        | 1            |
+--------------------------+

but I want it to return:
Code:
___________________________
| Ballot.id | Vote.user_id |
------------+--------------|
|  1        | 1            |
|  2        | NULL         |
+--------------------------+

How can I do this? I see that joins won't work.
 
SELECT B.id, V.user_id
FROM ballots AS B
LEFT JOIN votes AS V
ON B.id=V.ballot_id AND V.user_id='1'

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
ahh! Thanks, I don't know why I didn't think of that. That worked consummately!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top