MYSQL 利用concat函数 生成更新或者插入SQL

有时候需要批量运维一批数据,一条一条写SQL比较麻烦,可以使用下面的方法批量生成

select sales_order_number,
       a.sog_line_id,
       actual_price,
       sales_goods_unit_price,
       b.id,
       concat('update your_table set actual_price=', sales_goods_unit_price, ' where id=',
              b.id, ' and sog_line_id=', a.sog_line_id, ';', '-- ', sales_order_number) as updateSql
from table_a a
         left join table_b b on a.sog_line_id = b.sog_line_id
where sales_order_number in (
    ''
    );


select so.sales_order_number,
       so.so_head_id,
       concat('INSERT INTO your_table (so_head_id, sog_line_id, sku_id,actual_price,item_discount,
unit_price,quantity, biz_id,currency,price_verification_status) VALUES (', so.so_head_id, ',', sog.sog_line_id, ',',
              sog.sales_goods_code, ',',
              sog.sales_goods_unit_price, ',', ifnull(sog.sales_goods_discount_price, 'null'), ',',
              ifnull(sog.sales_goods_unit_price, 'null'),
              ',', 1, ',', 12, ',', '''USD''', ',', 0, ')', ';') as insertsql
from table_a so
         join table_b sog on so.so_head_id = sog.so_head_id
where sog.so_head_id in
      (xxxxxx, xxxxxx);

select sales_order_number,
       a.sog_line_id,
       actual_price,
       sales_goods_unit_price,
       b.id,
       concat('update your_table set actual_price=', sales_goods_unit_price, ' where id=',
              b.id, ' and sog_line_id=', a.sog_line_id, ';', '-- ', sales_order_number) as updateSql
from table_a a
         left join table_b b on a.sog_line_id = b.sog_line_id
where sales_order_number in (
    ''
    );


select so.sales_order_number,
       so.so_head_id,
       concat('INSERT INTO your_table (so_head_id, sog_line_id, sku_id,actual_price,item_discount,
unit_price,quantity, biz_id,currency,price_verification_status) VALUES (', so.so_head_id, ',', sog.sog_line_id, ',',
              sog.sales_goods_code, ',',
              sog.sales_goods_unit_price, ',', ifnull(sog.sales_goods_discount_price, 'null'), ',',
              ifnull(sog.sales_goods_unit_price, 'null'),
              ',', 1, ',', 12, ',', '''USD''', ',', 0, ')', ';') as insertsql
from table_a so
         join table_b sog on so.so_head_id = sog.so_head_id
where sog.so_head_id in
      (xxxxxx, xxxxxx);

concat怎么给查出来的字段加上单引号

在SQL中,CONCAT 函数通常用于将多个字符串连接成一个字符串。如果你需要给 CONCAT 的结果字段添加单引号,你可以在字段外部使用单引号来实现。

例如,假设你有一个名为 users 的表,其中包含 first_name 和 last_name 两个字段,你可以使用以下SQL语句来连接这两个字段,并在连接后的字符串两侧加上单引号:

SELECT CONCAT('\'', first_name, ' ', last_name, '\'') AS full_name FROM users;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值