开发者

SQL Update - Adding to String to One Attribute

开发者 https://www.devze.com 2023-03-15 19:42 出处:网络
My mysql table is : P开发者_运维百科roduct (name,price,metaDescription) I want to write an SQL UPDATE to Set my metaDescription name+\' is only just for\'+metaDescription

My mysql table is : P开发者_运维百科roduct (name,price,metaDescription)

I want to write an SQL UPDATE to Set my metaDescription name+' is only just for'+metaDescription

I tried this but it didn't work

UPDATE 
  product 
SET 
  metaDescription=name+' is just for'+price;


You should use the concat function:

update product
set metaDescription = concat(name, ' is just for ', price);

MySQL should automatically convert price to a string type for you.

MySQL is trying to convert your strings to numbers (and silently failing) when you use +:

mysql> select 'this' + 'that';
+-----------------+
| 'this' + 'that' |
+-----------------+
|               0 |
+-----------------+
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号