我有一个使用SELECT语句插入的查询:

INSERT INTO courses (name, location, gid) 
SELECT name, location, gid 
FROM courses 
WHERE cid = $cid

是否可以仅为插入选择“名称,位置”,并将gid设置为查询中的其他内容?


当前回答

语法正确:选择拼写错误

INSERT INTO courses (name, location, gid)
SELECT name, location, 'whatever you want' 
FROM courses 
WHERE cid = $ci 

其他回答

正确的查询语法是:

INSERT INTO courses (name, location, gid) 
SELECT (name, location, gid) 
FROM courses 
WHERE cid = $cid

是的,当然,但是请检查一下语法。

INSERT INTO courses (name, location, gid)
SELECT name, location, 1
FROM   courses
WHERE  cid = 2

您可以在其位置放置与gid相同类型的常量,当然不只是1。这是我编的cid值。

我们都知道这很有效。

INSERT INTO `TableName`(`col-1`,`col-2`)
SELECT  `col-1`,`col-2` 

=========================== 下面的方法可以在多个“select”语句的情况下使用。只是为了提供信息。

INSERT INTO `TableName`(`col-1`,`col-2`)
 select 1,2  union all
 select 1,2   union all
 select 1,2 ;

是的,它是。你可以这样写:

INSERT INTO courses (name, location, gid) 
SELECT name, location, 'whatever you want' 
FROM courses 
WHERE cid = $ci

或者你可以从选择的另一个连接中获取值…

语法正确:选择拼写错误

INSERT INTO courses (name, location, gid)
SELECT name, location, 'whatever you want' 
FROM courses 
WHERE cid = $ci