使用选择插入

发布于 2024-10-24 19:31:34 字数 230 浏览 6 评论 0原文

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

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

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

I have a query that inserts using a SELECT statement:

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

Is it possible to only select "name, location" for the insert, and set gid to something else in the query?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

奶茶白久 2024-10-31 19:31:34

是的,绝对可以,但请检查你的语法。

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

您可以在其位置放置一个与 gid 相同类型的常量,当然不仅仅是 1。而且,我刚刚编写了 cid 值。

Yes, absolutely, but check your syntax.

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

You can put a constant of the same type as gid in its place, not just 1, of course. And, I just made up the cid value.

給妳壹絲溫柔 2024-10-31 19:31:34

是的。您可以编写:

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

或者您可以从 select 的另一个连接获取值...

Yes, it is. You can write :

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

or you can get values from another join of the select ...

血之狂魔 2024-10-31 19:31:34

正确语法:选择拼写错误

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

Correct Syntax: select spelling was wrong

INSERT INTO courses (name, location, gid)
SELECT name, location, 'whatever you want' 
FROM courses 
WHERE cid = $ci 
冷…雨湿花 2024-10-31 19:31:34

当然,你想用 gid 做什么?静态值,PHP var,...

静态值 1234 可能类似于:

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

Sure, what do you want to use for the gid? a static value, PHP var, ...

A static value of 1234 could be like:

INSERT INTO courses (name, location, gid)
SELECT name, location, 1234
FROM courses
WHERE cid = $cid
梦太阳 2024-10-31 19:31:34

当然可以。

不过,应该注意一件事:INSERT INTO SELECT 语句从一个表复制数据并将其插入到另一个表中并且要求源表和目标表中的数据类型匹配。如果给定表列中的数据类型不匹配(即尝试将 VARCHAR 插入 INT 中,或将 TINYINT 插入 INT 中>) MySQL 服务器将抛出 SQL 错误 (1366)

所以要小心。

以下是该命令的语法:

INSERT INTO table2 (column1, column2, column3)
SELECT column1, column2, column3 FROM table1
WHERE condition;

旁注:有一种方法可以通过使用 SELECT 中进行转换,例如:

SELECT CAST('qwerty' AS CHAR CHARACTER SET utf8) COLLATE utf8_bin;

此转换(CAST()CONVERT( ) ) 如果您的表在同一表列上具有不同的字符集(如果处理不当,可能会导致数据丢失),则非常有用。

Of course you can.

One thing should be noted however: The INSERT INTO SELECT statement copies data from one table and inserts it into another table AND requires that data types in source and target tables match. If data types from given table columns does not match (i.e. trying to insert VARCHAR into INT, or TINYINT intoINT) the MySQL server will throw an SQL Error (1366).

So be careful.

Here is the syntax of the command:

INSERT INTO table2 (column1, column2, column3)
SELECT column1, column2, column3 FROM table1
WHERE condition;

Side note: There is a way to circumvent different column types insertion problem by using casting in your SELECT, for example:

SELECT CAST('qwerty' AS CHAR CHARACTER SET utf8) COLLATE utf8_bin;

This conversion (CAST() is synonym of CONVERT() ) is very useful if your tables have different character sets on the same table column (which can potentially lead to data loss if not handled properly).

巨坚强 2024-10-31 19:31:34

我们都知道这有效。

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 ;

We all know this works.

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

===========================
Below method can be used in case of multiple "select" statements. Just for information.

INSERT INTO `TableName`(`col-1`,`col-2`)
 select 1,2  union all
 select 1,2   union all
 select 1,2 ;
爱给你人给你 2024-10-31 19:31:34

您的查询的正确语法是:

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

The right Syntax for your query is:

INSERT INTO courses (name, location, gid) 
SELECT (name, location, gid) 
FROM courses 
WHERE cid = $cid
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文