开发者

How to use SELECT INTO with static values included?

开发者 https://www.devze.com 2023-03-01 13:25 出处:网络
I\'d like to add a value to the values provided by a SELECT INTO statement. Given the table named foo:

I'd like to add a value to the values provided by a SELECT INTO statement.

Given the table named foo:

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

I would like to populate the table bar with:

+----+------+
| id | type |
+----+------+
| 1  | R    |
| 2  | R    |开发者_开发百科
| 3  | R    |
+----+------+

I'd imagine this looking something like this:

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

... unfortunately this doesn't work. Can anyone tell me how to do this please

I'm using both mysql and mssql


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

In MySQL this would normally be done with:

Lazy with no indexes

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

Nicer way (assuming you've created table bar already)

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


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

For MySQL use:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号