如何选择包含1列计数的多列

时间:2015-09-25 12:56:41

标签: mysql sql

我有两个表,即tbl_votes和tbl_candidates:

tbl_votes:包含ff列:

voteID    president    vicePresident    secretary    treasurer    rep1    rep2     rep3
1             1              3                                     9       12       15
2             1              4              6            8         12      15
3             2              3              5            7         9       12

而tbl_candidates包含ff列:

idcandidate  fName         mName         lName         position
1            Jefferson     Garcia        Roxas         president
2            Carlson       Mcvoy         Zafiro        president
3            Andrew        Efron         Anderson      vice president
4            Jessica       Garcia        Roxas         secretary

...

9            Jayson        Sanchez       Dela Cruz      representative
10           Arille Alisa  Villanueva    Valdez         representative
11           Arnold        Castro        Baltazar       representative
12           Kristina      de Leon       Guillermo      representative
13           Jayson        Jones         Dela Cruz      representative
14           Karla         Santos        Abrogena       representative
15           Jason                       Chua           representative

我想要的查询是:

Name                                                       votes
Jefferson Garcia Roxas                                       2
Carlson Mcvoy Zafiro                                         1
Andrew Efron Anderson                                        2
Jessica Garcia Roxas                                         1

...

Jayson Sanchez Dela Cruz                                     2
Arnold Castro Baltazar                                       0
Kristina de Leon Guillermo                                   3
Jason  Chua                                                  2

我被困在哪里数票,这是我的代码:

SELECT `Name`, Votes
FROM (SELECT COUNT(*) as Votes FROM tbl_votes) a
CROSS JOIN (Select (Concat(fName ,' ',mName,' ', lName)) as `Name` from tbl_candidates) b;

此查询的结果是:

Name                                                       Votes
Jefferson Garcia Roxas                                       3
Carlson Mcvoy Zafiro                                         3
Andrew Efron Anderson                                        3
Jessica Garcia Roxas                                         3
Jayson Sanchez Dela Cruz                                     3
Arnold Castro Baltazar                                       3
Kristina de Leon Guillermo                                   3
Jason  Chua                                                  3

我将如何计算特定于idcandidate的计数? 非常感谢您的大力帮助。谢谢!

3 个答案:

答案 0 :(得分:2)

虽然 可以<{1}}(但与你提出的不同)并且对结果进行适当的汇总,但这是一种不能很好扩展的糟糕方法。当然,当你背负着糟糕的数据模型时,没有真正好的方法,就像你一样。

有几种方法可以解决这个问题,其中没有一种方法特别好,例如:

CROSS JOIN

按位置分解问题,使用每个位置的一个内联视图为每​​个候选人为该位置生成一行。然后它通过SELECT `Name`, COUNT(*) AS `votes` FROM ( SELECT CONCAT(fName, ' ', mName, ' ', lName) as `Name` FROM tbl_candidates c JOIN tbl_votes v ON c.idcandidate = v.president WHERE c.position = 'president' UNION ALL SELECT CONCAT(fName, ' ', mName, ' ', lName) as `Name` FROM tbl_candidates c JOIN tbl_votes v ON c.idcandidate = v.vicePresident WHERE c.position = 'vice president' UNION ALL SELECT CONCAT(fName, ' ', mName, ' ', lName) as `Name` FROM tbl_candidates c JOIN tbl_votes v ON c.idcandidate IN (v.rep1, v.rep2, v.rep3) WHERE c.position = 'representative' ) vote_agg GROUP BY `Name` 将它们组合成一个整体列表,并对结果执行聚合查询以计算每个候选人的投票。

如果现有候选人对他们没有竞选的职位有任何投票(通过对指定数据模型的约束很难或无法阻止),那么这些将被忽略。如果任何一张选票对同一代表候选人有多于一票,那么只会计算一个(可能是期望的行为,也许不是)。

答案 1 :(得分:1)

SELECT fName||' '||mName||' '||lName, votes FROM (
SELECT a.idcandidate, COUNT(b.idcandidate) votes
FROM tbl_candidates a
LEFT JOIN (
SELECT president idcandidate from tbl_votes
UNION ALL
SELECT vicePresident idcandidate from tbl_votes
UNION ALL
SELECT secretary idcandidate from tbl_votes
UNION ALL
SELECT treasurer idcandidate from tbl_votes
UNION ALL
SELECT rep1 idcandidate from tbl_votes
UNION ALL
SELECT rep2 idcandidate from tbl_votes
UNION ALL
SELECT rep3 idcandidate from tbl_votes ) b
ON (a.idcandidate = b.idcandidate)
GROUP BY a.idcandidate ) tab 
JOIN tbl_candidates b on (b.idcandidate = tab.idcandidate)

上面的答案是针对SQLite的,我在某种程度上误读了这个问题上的标签。

然而它可能有效,除了第一行需要采用mySQL格式:

SELECT CONCAT_WS(" ", fName, mName, lName), votes FROM (

答案 2 :(得分:0)

你的模型有问题......一旦你解决这些问题,创建起来就非常简单。

... Minimaly:

我会创建一个职位表,以便我可以直接与候选人联系。

positons_table
id     position
1      president
2      vice-president
3      secretary
4      ....

然后候选人表...

idcandidate  fName         mName         lName         position
1            Jefferson     Garcia        Roxas         1
2            Carlson       Mcvoy         Zafiro        1
3            Andrew        Efron         Anderson      2
4            Jessica       Garcia        Roxas         3

然后,如果你不想改变你的tbl_votes ......

总统投票:

SELECT CONCAT(c.fName, " ", c.mName, " ", c.lName) as Name, count(v.president) as votes 
FROM tbl_votes v
INNER_JOIN tbl_candidates c on v.president = c.idcandidate GROUP BY v.president

副总统的投票

SELECT CONCAT(c.fName, " ", c.mName, " ", c.lName) as Name, count(v.vicePresident) as votes 
FROM tbl_votes v
INNER_JOIN tbl_candidates c on v.vicePresident = c.idcandidate GROUP BY v.vicePresident

依旧...... 如果你需要总结一下......

SELECT CONCAT(c.fName, " ", c.mName, " ", c.lName) as Name, count(v.president) as votes 
FROM tbl_votes v
INNER_JOIN tbl_candidates c on v.president = c.idcandidate GROUP BY v.president

UNION

SELECT CONCAT(c.fName, " ", c.mName, " ", c.lName) as Name, count(v.vicePresident) as votes 
FROM tbl_votes v
INNER_JOIN tbl_candidates c on v.vicePresident = c.idcandidate GROUP BY v.vicePresident

UNION ....