关于子查询多行插入的问题

发布于 2024-09-15 12:59:16 字数 607 浏览 7 评论 0原文

假设我有以下 2 个表,

CREATE TABLE t1(
  name VARCHAR(25) NOT NULL,
  time INT,
  a INT
);

CREATE TABLE t2(
  name VARCHAR(25) NOT NULL,
  time INT,
  b INT
);

并且我希望在给定时间从 t1 中提取所有值 (a),前一个时间的所有值(例如为了方便起见,只需使用 time-1 ),然后对于每个名称,从旧名称中减去新名称,并将这些值插入具有相同 时间t2 中。执行此操作的缓慢方法将涉及执行类似的操作

SELECT name, a FROM t1 WHERE time = x;
SELECT name, a FROM t1 WHERE time = x-1;
(subtract the as for each name)
INSERT INTO t2 VALUES ....;

从我对子查询的(有限)理解,希望应该有一种方法可以在一个查询中完成所有这一切。有什么想法吗?提前致谢 :)

Say I have the following 2 tables,

CREATE TABLE t1(
  name VARCHAR(25) NOT NULL,
  time INT,
  a INT
);

CREATE TABLE t2(
  name VARCHAR(25) NOT NULL,
  time INT,
  b INT
);

and Im looking to pull all the values (a) out of t1 with a given time, all the values with the previous time (say just time-1 for convenience) then for each name subtract the newer one from the older one and insert those values into t2 with the same time. The slow way of doing this would involve doing something like

SELECT name, a FROM t1 WHERE time = x;
SELECT name, a FROM t1 WHERE time = x-1;
(subtract the as for each name)
INSERT INTO t2 VALUES ....;

From my (limited) understanding of subqueries, there should hopefully be a way to do this all in 1 query. Any ideas? Thanks in advance :)

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

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

发布评论

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

评论(2

著墨染雨君画夕 2024-09-22 12:59:16

看起来您可以使用 INSERT ... SELECT 语法:

INSERT INTO t2 (name, time, b)
SELECT  ta.name, ta.time time, (ta.a - tb.a) b
FROM    t1 ta 
JOIN    t1 tb ON (tb.time = ta.time - 1 AND tb.name = ta.name);

测试用例:

INSERT INTO t1 VALUES ('t1', 1, 100);
INSERT INTO t1 VALUES ('t1', 2, 200);
INSERT INTO t1 VALUES ('t1', 3, 500);
INSERT INTO t1 VALUES ('t1', 4, 600);
INSERT INTO t1 VALUES ('t1', 5, 800);
INSERT INTO t1 VALUES ('t1', 6, 900);

结果:

SELECT * FROM t2;
+------+------+------+
| name | time | b    |
+------+------+------+
| t1   |    2 |  100 |
| t1   |    3 |  300 |
| t1   |    4 |  100 |
| t1   |    5 |  200 |
| t1   |    6 |  100 |
+------+------+------+
5 rows in set (0.00 sec)

It looks like you can use the INSERT ... SELECT syntax:

INSERT INTO t2 (name, time, b)
SELECT  ta.name, ta.time time, (ta.a - tb.a) b
FROM    t1 ta 
JOIN    t1 tb ON (tb.time = ta.time - 1 AND tb.name = ta.name);

Test case:

INSERT INTO t1 VALUES ('t1', 1, 100);
INSERT INTO t1 VALUES ('t1', 2, 200);
INSERT INTO t1 VALUES ('t1', 3, 500);
INSERT INTO t1 VALUES ('t1', 4, 600);
INSERT INTO t1 VALUES ('t1', 5, 800);
INSERT INTO t1 VALUES ('t1', 6, 900);

Result:

SELECT * FROM t2;
+------+------+------+
| name | time | b    |
+------+------+------+
| t1   |    2 |  100 |
| t1   |    3 |  300 |
| t1   |    4 |  100 |
| t1   |    5 |  200 |
| t1   |    6 |  100 |
+------+------+------+
5 rows in set (0.00 sec)
樱娆 2024-09-22 12:59:16

有我的mysql 插入...选择

INSERT INTO table ( fields )
SELECT fields FROM table;

there is im mysql insert ... select

INSERT INTO table ( fields )
SELECT fields FROM table;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文