如何使用包含静态值的SELECT INTO?

时间:2011-04-27 11:14:33

标签: mysql sql sql-server select-into

我想为SELECT INTO语句提供的值添加一个值。

给出名为foo的表:

+----+
| id |
+----+
| 1  |
| 2  |
| 3  |
+----+

我想用表格填充表格栏:

+----+------+
| id | type |
+----+------+
| 1  | R    |
| 2  | R    |
| 3  | R    |
+----+------+

我想这看起来像这样:

SELECT foo.id, type INTO bar FROM foo LEFT JOIN 'R' AS type;

......遗憾的是,这不起作用。谁能告诉我怎么做呢

我正在使用mysql和mssql

2 个答案:

答案 0 :(得分:10)

SELECT foo.id, 'R' AS type INTO bar FROM foo;

在MySQL中,这通常可以通过以下方式完成:

没有索引的懒惰

CREATE TABLE bar SELECT id, 'R' AS type FROM foo;

更好的方式(假设你已经创建了表bar

INSERT INTO bar SELECT id, 'R' AS type FROM foo;

答案 1 :(得分:4)

SELECT foo.id
     , 'R'
INTO bar (id, type)
FROM foo

对于MySQL使用:

INSERT INTO bar (id, type)
  SELECT foo.id
       , 'R'
  FROM foo