英文:
How loop through the rows for unique users and record the number of matches in another SQL table?
问题
我最近开始学习SQL,我正在尝试解决这个问题:
我有一个投票表格 db.votes
,它记录了昵称、时间和类型,就像事件日志一样。现在已经有 50 万行记录了。
我需要在另一个表格 db.users
的 total_votes
列中记录每个用户的总匹配数(他的总投票数)。
我可以手动获取用户的总投票数,例如:
SELECT COUNT(*) FROM voters WHERE username="john";
此外,我可以将获取到的值写入其他表格。
UPDATE users SET total_votes = 20 WHERE username="john";
但如何使用循环来自动完成呢?
我尝试了谷歌类似的方法,找到了一些关于 SQL-T 的选项,但我不确定这是否适合我。
英文:
I recently started to study SQL and I'm trying to solve the problem:
I have a voting table db.votes
, it records the nickname, time and type. Like an event log. It now has 500,000 lines.
I need to record the total number of matches for each user (his total number of votes) in another db.users
table in the total_votes
column.
I can get the total votes for a user manually, like:
SELECT COUNT(*) FROM voters WHERE username="john";
Also I can write down the received value in other table.
UPDATE users SET total_votes = 20 WHERE username="john";
But how to do it automatically with a loop?
I tried to google similar methods and found several options with SQL-T, but I'm not sure if this will suit me.
答案1
得分: 1
你可以使用相关子查询来实现这个,这也适用于多个用户。
CREATE TABLE users(username varchar(10), total_votes int)
CREATE TABLE voters(username varchar(10))
UPDATE users
SET total_votes = (SELECT COUNT(*) FROM voters WHERE username = users.username)
WHERE username="john";
> status > Rows matched: 0 Changed: 0 Warnings: 0 >
英文:
You can use a correlated sub query for that, that would also work with multiple user
CREATE tABLe users(username varchar(10), total_votes int)
CREATE TABLE voters(username varchar(10))
UPDATE users
SET total_votes = (SELECT COUNT(*) FROM voters WHERE username = users.username)
WHERE username="john";
> status
> Rows matched: 0 Changed: 0 Warnings: 0
>
答案2
得分: 1
以下是翻译好的内容:
您可以通过多种方式来实现这一点。其中一种方式如下所示:
with v (voter_id, vote_count) as
(
select voter_id, count(*)
from votes
group by voter_id
)
update users
inner join v on v.voter_id = users.id
set total_votes = v.vote_count;
这里有一个DBFiddle演示
编辑:我不是MariaDb用户,看起来MariaDb不支持更新操作的公共表达式(CTE)。您可以查看其文档以确认。无论如何,可以使用子查询来实现:
update users
inner join
(
select voter_id, count(*) as vote_count
from votes
group by voter_id
) v on v.voter_id = users.id
set users.total_votes = v.vote_count;
这是MariaDb 10.9的DBFiddle演示。
英文:
You can do this in a number of ways. One of them looks like this:
with v (voter_id, vote_count) as
(
select voter_id, count(*)
from votes
group by voter_id
)
update users
inner join v on v.voter_id = users.id
set total_votes = v.vote_count;
Here is a DBFiddle demo
EDIT: I am not a MariaDb user and looks like MariaDb does not support CTE for updates. You can check its documentation to be sure. Anyway, then a subquery works:
update users
inner join
(
select voter_id, count(*) as vote_count
from votes
group by voter_id
) v on v.voter_id = users.id
set users.total_votes = v.vote_count;
Here is DBFiddle demo for MariaDb 10.9.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论