简体   繁体   English

如何复制SQL表中的一行,将其插入同一表中,然后在名称末尾附加“ -Copy”?

[英]How to copy a row in an SQL table, insert it into the same table, and append “-Copy” to the end of the name?

I need to copy a full row in an SQL table but add "-Copy" to the end of the MarketName for the copied row. 我需要在SQL表中复制完整的行,但在复制的行的MarketName的末尾添加“ -Copy”。 I have the following: 我有以下内容:

$CopyMarket = "INSERT INTO Markets (MarketName, Size)
                     (SELECT MarketName, Size)
                     FROM Markets
                     WHERE MarketID=$CopyID";

How do I append "-Copy" to the MarketName? 如何在MarketName后面附加“ -Copy”?

ie: The name is "Colorado," I want to copy it and insert as a new row as "Colorado-Copy" 即:名称是“ Colorado”,我要复制它并作为“ Colorado-Copy”作为新行插入

Thanks. 谢谢。

Use CONCAT function from MySQL to append or prepend data. 使用MySQL的CONCAT函数添加或添加数据。 Also work in standard SQL. 也可以在标准SQL中使用。

Append 附加

$CopyMarket = "INSERT INTO Markets (MarketName, Size)
                 (SELECT CONCAT(MarketName, '-Copy'), Size)
                 FROM Markets
                 WHERE MarketID=$CopyID"; // Field-Copy

Prepend 前置

$CopyMarket = "INSERT INTO Markets (MarketName, Size)
                 (SELECT CONCAT('Copy-', MarketName), Size)
                 FROM Markets
                 WHERE MarketID=$CopyID"; // Copy-Field

From MySQL documentation : MySQL文档

Returns the string that results from concatenating the arguments. 返回连接参数产生的字符串。 May have one or more arguments. 可能有一个或多个参数。 If all arguments are nonbinary strings, the result is a nonbinary string. 如果所有参数均为非二进制字符串,则结果为非二进制字符串。 If the arguments include any binary strings, the result is a binary string. 如果参数包含任何二进制字符串,则结果为二进制字符串。 A numeric argument is converted to its equivalent binary string form; 数字参数将转换为等效的二进制字符串形式; if you want to avoid that, you can use an explicit type cast, as in this example: 如果要避免这种情况,可以使用显式类型强制转换,如以下示例所示:

SELECT CONCAT(CAST(int_col AS CHAR), char_col);

CONCAT() returns NULL if any argument is NULL. 如果任何参数为NULL,则CONCAT()返回NULL。

mysql> SELECT CONCAT('My', 'S', 'QL');
    -> 'MySQL'
mysql> SELECT CONCAT('My', NULL, 'QL');
    -> NULL
mysql> SELECT CONCAT(14.3);
    -> '14.3'

For quoted strings, concatenation can be performed by placing the strings next to each other: 对于带引号的字符串,可以通过将字符串彼此相邻放置来执行串联:

mysql> SELECT 'My' 'S' 'QL';
    -> 'MySQL'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 codeigniter 从数据库表中复制一行并将其插入到同一个表中 - How to copy a row from database table and insert it to the same table using codeigniter 将 mySQL 行复制到具有不同名称的完全相同的表? - Copy mySQL row to exact same table with different name? 如何将数据从同一行的一行复制到另一行? - How to copy data from one row into another, in the same table? Append / 从 javascript / jQuery 的数据库中复制带有下拉值的表行 - Append / copy table row with a dropdown value from database in javascript / jQuery 如何在mysql中使用php插入动态输入值将一张表同时复制到另一张表 - How to insert dynamic input values copy one table to another table at the same time in mysql using php 如何从一个表数据复制列到另一表并在mysql中同时插入更多列? - how to copy column from one table data to another table and insert more columns same time in mysql? Mysql同行拷贝 - 同一表120次 - Mysql same row copy-same table 120 times 复制整个Model的数据库并插入到同一个表中,并更改了列 - Copy an entire Model's database and insert into same table with columns changed 无法将行复制到同一表(php-mysql) - Unable to copy a row to the same table(php-mysql) PHP MySQL 在同一个表中复制一行...使用主键和唯一键 - PHP MySQL Copy a row within the same table... with a Primary and Unique key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM