Mysql update语句赋值嵌套与在表列中数据后面增加数据

点击(此处)折叠或打开

  1. update a set col=(select col from a where id='5') where id>5 and id<10;

报错了
ERROR 1093 (HY000): You can't specify target table 'a' for update in FROM clause

经过研究

发现是 mysql 定义update语句不能同时对同一张进行set 赋值操作,也就是说
update a 的时候 不能在后面select col from a ,如果是不同表操作是没有问题的。

想到一个解决方法:

点击(此处)折叠或打开

  1. update a set col=(select col from (select * from a ) as b where id='5' )where id>5 and id <10;

将select那里的a的表起一个别名b 就可以解决这个问题
 
 
update mem_player set `DataWarehouse`=(select `DataWarehouse` from (select * from mem_player) as b where `Pid`=100000) 

2、SQL— CONCAT(字符串连接函数)

有的时候,我们有需要将由不同栏位获得的资料串连在一起。每一种资料库都有提供方法来达到这个目的:

  • MySQL: CONCAT()
  • Oracle: CONCAT(), ||
  • SQL Server: +

CONCAT() 的语法如下:

CONCAT(字串1, 字串2, 字串3, ...): 将字串1、字串2、字串3,等字串连在一起。

请注意,Oracle的CONCAT()只允许两个参数;

换言之,一次只能将两个字串串连起来。不过,在Oracle中,我们可以用'||'来一次串连多个字串。

来看几个例子。假设我们有以下的表格:

Geography 表格

region_name store_name
East Boston
East New York
West Los Angeles
West San Diego

例子1:

MySQL/Oracle:
SELECT CONCAT(region_name,store_name) FROM Geography
WHERE store_name = 'Boston';

结果

'EastBoston'

例子2:

Oracle:
SELECT region_name || ' ' || store_name FROM Geography
WHERE store_name = 'Boston';

结果

'East Boston'

例子3:

SQL Server:
SELECT region_name + ' ' + store_name FROM Geography
WHERE store_name = 'Boston';

结果

'East Boston'

3、添加分隔符

concat_ws()函数, 表示concat with separator,即有分隔符的字符串连接

    如连接后以逗号分隔

        mysql> select concat_ws(',','11','22','33');

        +-------------------------------+

        | concat_ws(',','11','22','33') |

        +-------------------------------+

        | 11,22,33                      |

        +-------------------------------+

       1 row in set (0.00 sec)

   和concat不同的是, concat_ws函数在执行的时候,不会因为NULL值而返回NULL

        mysql> select concat_ws(',','11','22',NULL);

        +-------------------------------+

        | concat_ws(',','11','22',NULL) |

        +-------------------------------+

        | 11,22                         |

        +-------------------------------+

       1 row in set (0.00 sec)

下面是我的使用
  1. UPDATE `trace_sys`.`goods_production` SET `image` =
  2. (SELECT CONCAT_WS("?",image,"1") FROM (SELECT * FROM goods_production ) AS b WHERE `goodsid` = '1' ) WHERE `goodsid` = '1';

 









posted @ 2014-10-27 19:04  Tim&Blog  阅读(1617)  评论(0编辑  收藏  举报