使用一组常量插入的 SQL 查询

发布于 2024-09-24 15:33:49 字数 494 浏览 2 评论 0原文

似乎应该对此进行查询,但我不知道该怎么做。

我有一个包含复合主键的表,其中包含我想要填充数据的两个字段,

我可以从一个表进行插入以填充一半的键,但我想用以下内容填充另一半一组常量 (0, 3, 5, 6, 9) 等...

所以最终结果看起来像这样

+--------------+
|AwesomeTable  |
+--------------+
| Id1   | Id2  |
| 1     | 0    |
| 1     | 3    |
| 1     | 5    |
| 1     | 6    |
| 1     | 9    | 
| 2     | 0    |
| 2     | 3    |
| ...   | ...  |
+--------------+   

我已经得到了 insert into Awesometable (id1, id2) select id1, [这里需要一些东西]来自表1 [这里需要其他东西]

It seems like there should be a query for this, but I can't think of how to do it.

I've got a table with a composite primary key consisting of two fields I'd like to populate with data,

I can do an insert into from one table to fill up half the keys, but I want to fill up the other half with a set of constants (0, 3, 5, 6, 9) etc...

so the end result would look like this

+--------------+
|AwesomeTable  |
+--------------+
| Id1   | Id2  |
| 1     | 0    |
| 1     | 3    |
| 1     | 5    |
| 1     | 6    |
| 1     | 9    | 
| 2     | 0    |
| 2     | 3    |
| ...   | ...  |
+--------------+   

I've got as far as insert into awesometable (id1, id2) select id1, [need something here] from table1 [need something else here]

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

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

发布评论

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

评论(2

爺獨霸怡葒院 2024-10-01 15:33:49

我有一个有 2 个主键的表

不,你没有。一张表只能有一个主键。您可能指的是复合主键。

我相信你想要这个:

INSERT
INTO    awesometable (id1, id2)
SELECT  t1.id1, q.id2
FROM    table1 t1
CROSS JOIN
        (
        SELECT  0 AS id2
        UNION ALL
        SELECT  3
        UNION ALL
        SELECT  5
        UNION ALL
        SELECT  6
        UNION ALL
        SELECT  9
        ) q

,或者在 Oracle 中:

INSERT
INTO    awesometable (id1, id2)
SELECT  t1.id1, q.id2
FROM    table1 t1
CROSS JOIN
        (
        SELECT  0 AS id2
        FROM    dual
        UNION ALL
        SELECT  3
        FROM    dual
        UNION ALL
        SELECT  5
        FROM    dual
        UNION ALL
        SELECT  6
        FROM    dual
        UNION ALL
        SELECT  9
        FROM    dual
        ) q

I've got a table with 2 primary keys

No, you don't. A table can only have one primary key. You probably mean a composite primary key.

I believe you want this:

INSERT
INTO    awesometable (id1, id2)
SELECT  t1.id1, q.id2
FROM    table1 t1
CROSS JOIN
        (
        SELECT  0 AS id2
        UNION ALL
        SELECT  3
        UNION ALL
        SELECT  5
        UNION ALL
        SELECT  6
        UNION ALL
        SELECT  9
        ) q

, or in Oracle:

INSERT
INTO    awesometable (id1, id2)
SELECT  t1.id1, q.id2
FROM    table1 t1
CROSS JOIN
        (
        SELECT  0 AS id2
        FROM    dual
        UNION ALL
        SELECT  3
        FROM    dual
        UNION ALL
        SELECT  5
        FROM    dual
        UNION ALL
        SELECT  6
        FROM    dual
        UNION ALL
        SELECT  9
        FROM    dual
        ) q
赠我空喜 2024-10-01 15:33:49

如果我理解正确的话,也许你可以使用这样的东西:

insert into awesometable (id1, id2) 
select id1, (select top 1 id2 from table2 where /*a condition here to retreive only one result*/) 
from table1

If I understand correctly, maybe you can use something like this:

insert into awesometable (id1, id2) 
select id1, (select top 1 id2 from table2 where /*a condition here to retreive only one result*/) 
from table1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文