简体   繁体   English

SQL-计算一列与另一列匹配的行(对)

[英]SQL - count rows where one column matches another (pairs)

I have a voting system on a website where every user can vote for a pair of users (female and male) to win. 我在一个网站上有一个投票系统,每个用户都可以投票给一对用户(女性和男性)投票。

The votes are stored in a table like the one below. 选票存储在下面的表格中。

voter_id |  voted_for | cell
90       |  30        | f
90       |  68        | m
42       |  30        | f
42       |  68        | m
111      |  74        | f
111      |  20        | m
45       |  120       | f
16       |  90        | f
16       |  135       | m
122      |  30        | f
122      |  68        | m
45       |  116       | m
46       |  30        | m
46       |  121       | f

Now, I want to count how many users voted for every pair. 现在,我要计算每对有多少用户投票。 Eg '3 votes for pair 30&68', '1 vote for pair 74&20', '1 vote for 120&116' and so on... 例如,“对30&68对3票”,“对74&20对1票”,“ 120和116对1票”,依此类推...

I have a SQL query which counts every user mentioned in voted_for but this doesn't include the pair-thing. 我有一个SQL查询,该查询会计算voted_for中提到的每个用户,但这不包括配对事物。 It would give me 4 votes for user 30 but user 30 although is in the 30&121 pair. 它将为用户30给我4票,但用户30虽然在30&121对中。

SELECT voted_for, count(*) AS count
FROM votes
GROUP BY voted_for
ORDER BY count desc

First transform your table like this: 首先像这样转换表:

| voter_id | voted_for_f | voted_for_m |
|----------|-------------|-------------|
|       16 |          90 |         135 |
|       42 |          30 |          68 |
|       45 |         120 |         116 |
|       46 |          30 |         121 |
|       90 |          30 |          68 |
|      111 |          74 |          20 |
|      122 |          30 |          68 |

You can do this with this query, which joins the table with itself: 您可以使用以下查询执行此操作,该查询将表与其自身连接:

SELECT f.voter_id,
       f.voted_for AS voted_for_f,
       m.voted_for AS voted_for_m
FROM votes AS f
JOIN votes AS m ON f.voter_id = m.voter_id
               AND f.cell = 'f'
               AND m.cell = 'm'

Then you can use this query to count on it: 然后,您可以使用此查询来依靠它:

SELECT f.voted_for AS voted_for_f,
       m.voted_for AS voted_for_m,
       COUNT(*) AS number_of_votes
FROM votes AS f
JOIN votes AS m ON f.voter_id = m.voter_id
               AND f.cell = 'f'
               AND m.cell = 'm'
GROUP BY voted_for_f,
         voted_for_m

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何计算一列与另一张表的一列匹配的行数 - How to COUNT the number of rows where a column matches a column of another table SQL查询以查找其中1列的值与另一行的另一列匹配的行 - SQL query to find rows where 1 column value matches another column on another row 如何识别一个 ID 列在两行中匹配的记录,而另一列填充在一条记录中,但另一条记录中没有? - How to identify records where one ID column matches in both rows, and another column is populated in one record but not the other? 如何使用SQL对一列的值&lt;= x和另一列&gt; x的值的行数进行分组和计数? - How can I use SQL to group and count the number of rows where the value for one column is <= x and the value for another column > x? sql-server,计算列匹配字符串且id不重复的行 - sql-server, count rows where column matches string and id is not repeated SQL获取行值,其中该行值的行数与该行值的列匹配 - SQL get row value where count of rows for that row value matches with of the column of that row-value SQL计算列中每个不同的值,并从ID匹配的另一个表中添加名称 - SQL count each distinct value in column and add name from another table where the ID matches SQL - 一个表的行是另一个表的列标题的位置 - SQL - Pivoting where the rows of one table are the column titles of another SQL:计算行数,其中列 = 一个值且另一列与第一个条件为真的组中的值相同? - SQL: count rows where column = a value AND another column is the same as values in the group where the first condition is true? SQL查询-排除具有多个值的列的一个值匹配的行 - SQL Query - excluding rows where one value of a column with multiple values matches
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM