Solved

MySQL select and count from multiple tables.

Posted on 2007-04-04
6
1,187 Views
Last Modified: 2008-09-08
I have a database structure:
Table: choices
Fields: CID, PID(FK), Choice
Table: voteRecord
Fields: VRID, CID, UID, PID
Table: users
fields: UID, Username
Table: polls
fields: PID, pollQuestion

I wish to select the choice field from choices as well as counting how many times it is in VRID. The closest I have came is:
SELECT choice, count(voteRecord.CID)
FROM choices, voteRecord, users, polls
WHERE choices.PID=2 and voteRecord.UID=users.UID and voteRecord.CID=choices.CID and voteRecord.PID=polls.PID
GROUP BY voteRecord.CID

This provides the answer:
option 1 :: 3
option 2  :: 2
option 3 :: 1
option 4 :: 2

wheras it should be 1:1:1:2 as the number of votes per option.

I want to do this so that even if  for example option one is not selected whenever I go to output the results in java then an empty value is output and it keeps the tables alignd. (this is to be output into tables in a servlet)


Thanks,
Pete
0
Comment
Question by:pete420
  • 2
6 Comments
 

Expert Comment

by:cvonrabe
ID: 18854969
I would do something like this:

SELECT choice,
sum(if(voteRecord.CID=1,1,0)) as option_1,
sum(if(voteRecord.CID=2,1,0)) as option_2,
sum(if(voteRecord.CID=3,1,0)) as option_3,
sum(if(voteRecord.CID=4,1,0)) as option_4
FROM choices, voteRecord,users,polls
WHERE choices.PID=2 and voteRecord.UID=users.UID and voteRecord.CID=choices.CID
and voteRecord.PID = polls.PID
GROUP BY voteRecord.CID

Using the above output like:

+--------+----------+----------+----------+----------+
| choice | option_1 | option_2 | option_3 | option_4 |
+--------+----------+----------+----------+----------+
| red      |        3     |        0      |        0      |        0      |
| blue     |        0    |        2       |        0      |        0      |
| pink     |        0    |        0       |        1      |        0      |
| black   |        0    |        0       |        0      |        1      |
+--------+----------+----------+----------+----------+
4 rows in set (0.00 sec)

Of course, you could dynamically build the query.
0
 

Author Comment

by:pete420
ID: 18856579
Hi,

Thanks for the reply. I don't think I have explained it to well tho.

The answer should simply read:
+--------+---------------
|Choice | count(CID)
+--------+---------------
Red               1
Blue               1
Black             1
Green            2

Thanks,
Pete

The number of votes here are just used as an example. It could easily be red: 200, blue 1200, etc etc.
The system is a voting system.
 
0
 
LVL 2

Accepted Solution

by:
taveirne earned 75 total points
ID: 18860189
select c.choice, ifnull(tbl.cnt, 0)
from choices c
left join (
   select cid, count(cid) as cnt
   from voterecord
   where pollid = X
   group by cid
) tbl on tbl.cid = c.cid
0
 

Expert Comment

by:cvonrabe
ID: 18860787
That seems to have done the trick at my end.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been using r1soft Continuous Data Protection (http://www.r1soft.com/linux-cdp/) for many years now with the mySQL Addon and wanted to share a trick I have used several times. For those of us that don't have the luxury of using all transact…
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question