我有一个使用SELECT语句插入的查询:
INSERT INTO courses (name, location, gid)
SELECT name, location, gid
FROM courses
WHERE cid = $cid
是否可以仅为插入选择“名称,位置”,并将gid设置为查询中的其他内容?
我有一个使用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 `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
或者你可以从选择的另一个连接中获取值…
正确的查询语法是:
INSERT INTO courses (name, location, gid)
SELECT (name, location, gid)
FROM courses
WHERE cid = $cid
当然,gid要用什么?一个静态值,PHP var,…
1234的静态值可能是这样的:
INSERT INTO courses (name, location, gid)
SELECT name, location, 1234
FROM courses
WHERE cid = $cid