gpt4 book ai didi

mysql - 使用 MySql 将 COUNT 语句的输出插入到表中。

转载 作者:行者123 更新时间:2023-11-29 19:08:21 25 4
gpt4 key购买 nike

我一直在努力想出解决以下问题的方法。

我有两个具有以下结构的表。

member table:

╔═══════════╦═════════════╦═════════╗
║ member_no ║ member_type ║ team_no ║
╠═══════════╬═════════════╬═════════╣
║ CON123 ║ monthly ║ 12 ║
╠═══════════╬═════════════╬═════════╣
║ CON456 ║ monthly ║ 13 ║
╠═══════════╬═════════════╬═════════╣
║ CON789 ║ annual ║ 13 ║
╚═══════════╩═════════════╩═════════╝

team table:

╔═════════╦════════════╦══════════════╗
║ team_no ║ supervisor ║ member_count ║
╠═════════╬════════════╬══════════════╣
║ 12 ║ John ║ 1 ║
╠═════════╬════════════╬══════════════╣
║ 13 ║ Joe ║ 2 ║
╠═════════╬════════════╬══════════════╣
║ 14 ║ Allan ║ 0 ║
╚═════════╩════════════╩══════════════╝

在将值插入“团队”表时,我想做的是这样的事情:

INSERT INTO team (team_no , supervisor , member_count) 
VALUES ("13" , "Joe" , SELECT COUNT(team_no) FROM member WHERE team_no = "13");

本质上,我希望使用成员表上的 COUNT 函数将属于该团队的成员总数填充到“团队”表中的member_count 列中。

如果有人能帮助我解决这个问题,我将非常感激。

最佳答案

您可以使用此语法来使用 INSERT/SELECT

INSERT INTO team (team_no , supervisor , member_count) 
select "13" , "Joe" , COUNT(team_no)
FROM member
WHERE team_no = "13"

有关更多信息,请参阅 mysql 文档 https://dev.mysql.com/doc/refman/5.7/en/insert-select.html

关于mysql - 使用 MySql 将 COUNT 语句的输出插入到表中。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43309257/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com