Click here to get back home

join woes

 HomeNewsGroups | Search | About
 mailing.database.mysql    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
join woes Dave 03-26-2007
Get Chitika Premium
Posted by Dave on March 26, 2007, 5:16 am
Please log in for more thread options
I have a database with 3 tables, one for nominees, one for voters and one
for votes.

I used the following syntax

SELECT
`tblnominees`.`fldNominee`,
Count(`tblvotes`.`fldVote`) AS `COUNT_OF_VOTES`
FROM
`tblnominees`
Left Join `tblvotes` ON `tblvotes`.`fldVote` = `tblnominees`.`nomineeID`
GROUP BY
`tblnominees`.`fldNominee`
ORDER BY
`COUNT_OF_VOTES` DESC

and this worked well to give a list of all candidates, whether they had
votes or not (it returned '0' for those with no votes, which was perfect).

But now I have a need to use the voters table as well so I can use an ignore
field to exclude voters we don't want to count for whatever reason. I can't
work out how to do the joins so i still get the full list of nominees with
their vote count, whether thay have votes or not.

Could anyone shed some light please?

Appreciate the help.



Posted by strawberry on March 26, 2007, 2:07 pm
Please log in for more thread options
> I have a database with 3 tables, one for nominees, one for voters and one
> for votes.
>
> I used the following syntax
>
> SELECT
> `tblnominees`.`fldNominee`,
> Count(`tblvotes`.`fldVote`) AS `COUNT_OF_VOTES`
> FROM
> `tblnominees`
> Left Join `tblvotes` ON `tblvotes`.`fldVote` = `tblnominees`.`nomineeID`
> GROUP BY
> `tblnominees`.`fldNominee`
> ORDER BY
> `COUNT_OF_VOTES` DESC
>
> and this worked well to give a list of all candidates, whether they had
> votes or not (it returned '0' for those with no votes, which was perfect).
>
> But now I have a need to use the voters table as well so I can use an ignore
> field to exclude voters we don't want to count for whatever reason. I can't
> work out how to do the joins so i still get the full list of nominees with
> their vote count, whether thay have votes or not.
>
> Could anyone shed some light please?
>
> Appreciate the help.

At a guess:

SELECT nominees.nominee,count(votes.vote) as total FROM voters
LEFT JOIN votes ON votes.voter_id = voters.voter_id
LEFT JOIN nominees ON nominees.nominee_id = votes.nominee_id
WHERE voters.ignore = 0
GROUP BY votes.nominee
ORDER BY total DESC,nominees.nominee


Posted by Dave on March 27, 2007, 5:58 am
Please log in for more thread options
Thanks for the reply, but couldn't get this to work, with no votes present
there were no records from the query, and I was hoping to get all of the
nominees with '0' votes against each name.

>> I have a database with 3 tables, one for nominees, one for voters and one
>> for votes.
>>
>> I used the following syntax
>>
>> SELECT
>> `tblnominees`.`fldNominee`,
>> Count(`tblvotes`.`fldVote`) AS `COUNT_OF_VOTES`
>> FROM
>> `tblnominees`
>> Left Join `tblvotes` ON `tblvotes`.`fldVote` = `tblnominees`.`nomineeID`
>> GROUP BY
>> `tblnominees`.`fldNominee`
>> ORDER BY
>> `COUNT_OF_VOTES` DESC
>>
>> and this worked well to give a list of all candidates, whether they had
>> votes or not (it returned '0' for those with no votes, which was
>> perfect).
>>
>> But now I have a need to use the voters table as well so I can use an
>> ignore
>> field to exclude voters we don't want to count for whatever reason. I
>> can't
>> work out how to do the joins so i still get the full list of nominees
>> with
>> their vote count, whether thay have votes or not.
>>
>> Could anyone shed some light please?
>>
>> Appreciate the help.
>
> At a guess:
>
> SELECT nominees.nominee,count(votes.vote) as total FROM voters
> LEFT JOIN votes ON votes.voter_id = voters.voter_id
> LEFT JOIN nominees ON nominees.nominee_id = votes.nominee_id
> WHERE voters.ignore = 0
> GROUP BY votes.nominee
> ORDER BY total DESC,nominees.nominee
>



Posted by strawberry on March 27, 2007, 8:26 am
Please log in for more thread options
> Thanks for the reply, but couldn't get this to work, with no votes present
> there were no records from the query, and I was hoping to get all of the
> nominees with '0' votes against each name.
>
>
>
> >> I have a database with 3 tables, one for nominees, one for voters and one
> >> for votes.
>
> >> I used the following syntax
>
> >> SELECT
> >> `tblnominees`.`fldNominee`,
> >> Count(`tblvotes`.`fldVote`) AS `COUNT_OF_VOTES`
> >> FROM
> >> `tblnominees`
> >> Left Join `tblvotes` ON `tblvotes`.`fldVote` = `tblnominees`.`nomineeID`
> >> GROUP BY
> >> `tblnominees`.`fldNominee`
> >> ORDER BY
> >> `COUNT_OF_VOTES` DESC
>
> >> and this worked well to give a list of all candidates, whether they had
> >> votes or not (it returned '0' for those with no votes, which was
> >> perfect).
>
> >> But now I have a need to use the voters table as well so I can use an
> >> ignore
> >> field to exclude voters we don't want to count for whatever reason. I
> >> can't
> >> work out how to do the joins so i still get the full list of nominees
> >> with
> >> their vote count, whether thay have votes or not.
>
> >> Could anyone shed some light please?
>
> >> Appreciate the help.
>
> > At a guess:
>
> > SELECT nominees.nominee,count(votes.vote) as total FROM voters
> > LEFT JOIN votes ON votes.voter_id = voters.voter_id
> > LEFT JOIN nominees ON nominees.nominee_id = votes.nominee_id
> > WHERE voters.ignore = 0
> > GROUP BY votes.nominee
> > ORDER BY total DESC,nominees.nominee

Well I was close:

SELECT nominees.nominee, count( votes.nominee_id ) AS total
FROM voters
LEFT JOIN votes ON votes.voter_id = voters.voter_id
LEFT JOIN nominees ON nominees.nominee_id = votes.nominee_id
WHERE voters.ignore =0
GROUP BY votes.nominee_id
ORDER BY total DESC , nominees.nominee


Posted by Dave on March 28, 2007, 7:52 am
Please log in for more thread options
Unfortunately still no go!

>> Thanks for the reply, but couldn't get this to work, with no votes
>> present
>> there were no records from the query, and I was hoping to get all of the
>> nominees with '0' votes against each name.
>>
>>
>>
>> >> I have a database with 3 tables, one for nominees, one for voters and
>> >> one
>> >> for votes.
>>
>> >> I used the following syntax
>>
>> >> SELECT
>> >> `tblnominees`.`fldNominee`,
>> >> Count(`tblvotes`.`fldVote`) AS `COUNT_OF_VOTES`
>> >> FROM
>> >> `tblnominees`
>> >> Left Join `tblvotes` ON `tblvotes`.`fldVote` =
>> >> `tblnominees`.`nomineeID`
>> >> GROUP BY
>> >> `tblnominees`.`fldNominee`
>> >> ORDER BY
>> >> `COUNT_OF_VOTES` DESC
>>
>> >> and this worked well to give a list of all candidates, whether they
>> >> had
>> >> votes or not (it returned '0' for those with no votes, which was
>> >> perfect).
>>
>> >> But now I have a need to use the voters table as well so I can use an
>> >> ignore
>> >> field to exclude voters we don't want to count for whatever reason. I
>> >> can't
>> >> work out how to do the joins so i still get the full list of nominees
>> >> with
>> >> their vote count, whether thay have votes or not.
>>
>> >> Could anyone shed some light please?
>>
>> >> Appreciate the help.
>>
>> > At a guess:
>>
>> > SELECT nominees.nominee,count(votes.vote) as total FROM voters
>> > LEFT JOIN votes ON votes.voter_id = voters.voter_id
>> > LEFT JOIN nominees ON nominees.nominee_id = votes.nominee_id
>> > WHERE voters.ignore = 0
>> > GROUP BY votes.nominee
>> > ORDER BY total DESC,nominees.nominee
>
> Well I was close:
>
> SELECT nominees.nominee, count( votes.nominee_id ) AS total
> FROM voters
> LEFT JOIN votes ON votes.voter_id = voters.voter_id
> LEFT JOIN nominees ON nominees.nominee_id = votes.nominee_id
> WHERE voters.ignore =0
> GROUP BY votes.nominee_id
> ORDER BY total DESC , nominees.nominee
>



Similar ThreadsPosted
Need help with SQL join September 25, 2006, 2:41 pm
MYSQL Join help October 21, 2005, 12:14 pm
left join an sum October 25, 2005, 10:14 am
SUM(DISTINCT) OF INNER JOIN December 14, 2005, 8:06 am
join problem December 20, 2005, 6:27 pm
conditional join December 29, 2005, 2:17 am
How to join 2 queries into 1 March 10, 2006, 11:50 am
join tables? August 20, 2006, 3:21 am
Inner Join sub select query help October 14, 2005, 7:19 am
Self join to multiple references... October 25, 2005, 2:41 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap