Solved

MySQL select and count from multiple tables.

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

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…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

827 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