Solved

MySQL select and count from multiple tables.

Posted on 2007-04-04
6
1,174 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 Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
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://…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

758 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

20 Experts available now in Live!

Get 1:1 Help Now