I need to copy a full row in an SQL table but add "-Copy" to the end of the MarketName for the copied row. 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?
ie: The name is "Colorado," I want to copy it and insert as a new row as "Colorado-Copy"
Thanks.
Use CONCAT function from MySQL to append or prepend data. Also work in standard 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 :
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.
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'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.