MySQL:从子查询结果更新表列

发布于 2024-09-04 06:38:06 字数 506 浏览 1 评论 0原文

会员表上有“MemberID”和“PointsEarned”列。

我想根据此查询的结果更新 PointsEarned 列:

SELECT m.MemberID, m.UserName, 
     ( (SELECT COUNT(*) FROM EventsLog as e WHERE e.MemberID=m.MemberID AND e.EventsTypeID=2)*10 ) + 
     ( (SELECT COUNT(*) FROM EventsLog as e WHERE e.MemberID=m.MemberID AND e.EventsTypeID=3)*3 ) +
     ( (SELECT COUNT(*) FROM ChatMessages as c WHERE c.MemberID=m.MemberID)*.1 )
     as PointsEarned
   FROM Members as m

任何人都可以告诉我应该如何使用单个查询来完成此操作?

谢谢!

On the Members table are columns "MemberID" and "PointsEarned".

I want to update the PointsEarned column from the result of this query:

SELECT m.MemberID, m.UserName, 
     ( (SELECT COUNT(*) FROM EventsLog as e WHERE e.MemberID=m.MemberID AND e.EventsTypeID=2)*10 ) + 
     ( (SELECT COUNT(*) FROM EventsLog as e WHERE e.MemberID=m.MemberID AND e.EventsTypeID=3)*3 ) +
     ( (SELECT COUNT(*) FROM ChatMessages as c WHERE c.MemberID=m.MemberID)*.1 )
     as PointsEarned
   FROM Members as m

Can anybody tell me how I should do it with a single query?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

硪扪都還晓 2024-09-11 06:38:06

您可能想尝试以下操作:

UPDATE   Members m
SET      PointsEarned = 
     ( (SELECT COUNT(*) FROM EventsLog as e WHERE e.MemberID=m.MemberID AND e.EventsTypeID=2)*10 ) + 
     ( (SELECT COUNT(*) FROM EventsLog as e WHERE e.MemberID=m.MemberID AND e.EventsTypeID=3)*3 ) +
     ( (SELECT COUNT(*) FROM ChatMessages as c WHERE c.MemberID=m.MemberID)*.1 );

测试用例:

CREATE TABLE Members (MemberId int, PointsEarned int);
CREATE TABLE EventsLog (MemberID int, EventsTypeID int);
CREATE TABLE ChatMessages (MemberID int);

INSERT INTO Members VALUES (1, 0);
INSERT INTO Members VALUES (2, 0);
INSERT INTO Members VALUES (3, 0);

INSERT INTO EventsLog VALUES (1, 2);
INSERT INTO EventsLog VALUES (1, 2);
INSERT INTO EventsLog VALUES (1, 3);
INSERT INTO EventsLog VALUES (2, 2);
INSERT INTO EventsLog VALUES (3, 3);

INSERT INTO ChatMessages VALUES (1);
INSERT INTO ChatMessages VALUES (1);
INSERT INTO ChatMessages VALUES (2);

结果:

SELECT * FROM Members;
+----------+--------------+
| MemberId | PointsEarned |
+----------+--------------+
|        1 |           23 |
|        2 |           10 |
|        3 |            3 |
+----------+--------------+
3 rows in set (0.00 sec)

You may want to try the following:

UPDATE   Members m
SET      PointsEarned = 
     ( (SELECT COUNT(*) FROM EventsLog as e WHERE e.MemberID=m.MemberID AND e.EventsTypeID=2)*10 ) + 
     ( (SELECT COUNT(*) FROM EventsLog as e WHERE e.MemberID=m.MemberID AND e.EventsTypeID=3)*3 ) +
     ( (SELECT COUNT(*) FROM ChatMessages as c WHERE c.MemberID=m.MemberID)*.1 );

Test case:

CREATE TABLE Members (MemberId int, PointsEarned int);
CREATE TABLE EventsLog (MemberID int, EventsTypeID int);
CREATE TABLE ChatMessages (MemberID int);

INSERT INTO Members VALUES (1, 0);
INSERT INTO Members VALUES (2, 0);
INSERT INTO Members VALUES (3, 0);

INSERT INTO EventsLog VALUES (1, 2);
INSERT INTO EventsLog VALUES (1, 2);
INSERT INTO EventsLog VALUES (1, 3);
INSERT INTO EventsLog VALUES (2, 2);
INSERT INTO EventsLog VALUES (3, 3);

INSERT INTO ChatMessages VALUES (1);
INSERT INTO ChatMessages VALUES (1);
INSERT INTO ChatMessages VALUES (2);

Result:

SELECT * FROM Members;
+----------+--------------+
| MemberId | PointsEarned |
+----------+--------------+
|        1 |           23 |
|        2 |           10 |
|        3 |            3 |
+----------+--------------+
3 rows in set (0.00 sec)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文