Solved

MySQL select and count from multiple tables.

Posted on 2007-04-04
6
1,180 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MySQL - Limit or Top Records 15 42
User with DBO permission can't remotely connect 15 45
MySQL Init Waits 25 78
MySQL  on Tomcat 8 26
Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

911 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now