用SELECT进行INSERT
我有一个查询插入使用select:
INSERT INTO courses (name, location, gid) SELECT name, location, gid FROM courses WHERE cid = $cid
是否有可能只为插入select“名称,位置”,并设置为查询中的其他东西?
是的,绝对,但检查你的语法。
INSERT INTO courses (name, location, gid) SELECT name, location, 1 FROM courses WHERE cid = 2
当然,你可以把与gid
相同types的常量放在它的位置上,而不仅仅是1。 而且,我刚刚编造了cid
值。
是的。 你可以写 :
INSERT INTO courses (name, location, gid) SELECT name, location, 'whatever you want' FROM courses WHERE cid = $ci
或者你可以从另一个select的连接获取值…
正确的语法:select拼写错误
INSERT INTO courses (name, location, gid) SELECT name, location, 'whatever you want' FROM courses WHERE cid = $ci
当然,你想要什么使用的GID? 一个静态值,PHP的var,…
静态值1234可能是这样的:
INSERT INTO courses (name, location, gid) SELECT name, location, 1234 FROM courses WHERE cid = $cid
我认为你的INSERT语句是错误的,看到正确的语法: http : //dev.mysql.com/doc/refman/5.1/en/insert.html
编辑:正如安德鲁已经指出的…
您查询的正确语法是:
INSERT INTO courses (name, location, gid) SELECT (name, location, gid) FROM courses WHERE cid = $cid