Solved

MySQL select and count from multiple tables.

Posted on 2007-04-04
6
1,197 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Foreword This is an old article.  Instead of using the MySQL extension that was used in the original code examples, please choose one of the currently supported database extensions instead.  More information is available here: MySQLi / PDO (http://…
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

632 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