1. 程式人生 > >mysql concat函式批量操作語句

mysql concat函式批量操作語句

第一次見到這個函式是在開發那邊,柯文發來的修改資料的文件裡面包含了這個語句中包含的函式,剛見到還會以為裡面有delete不敢隨意執行。###查詢渠道人員總部離職人員-資料清理select t_sys_user.* ,concat('delete from t_channel_org_staff where flag = 1 and cityRegion = 20 and channelStaffId=',id,' ; ') as excuteSqlfrom t_sys_user where id in (select channelStaffId from t_channel_org_staff where flag = 1 and cityRegion = 20 )and t_sys_user.flag = 0;昨天,競拍那邊出現競拍車輛表和競拍訂單表的城市編號不一樣的情況,所有的競拍訂單表的城市編號全部為0;查詢相關的語句:select * from ams_car c ,ams_order o where c.flag=1 and c.overTime='2018-03-25 14:30:00' and c.carid=o.carid and o.detectionCity=0 ;select o.carId,o.detectionCity, c.detectionCity from ams_car c left join ams_order o on c.carid=o.carid where c.flag=1 and c.overTime='2018-03-25 14:30:00' and o.detectionCity=0 ;找出大概為320輛車的ams_order的城市編號為0;現在根據車編號對照更改訂單表裡的城市id為競拍車輛表的城市id。根據柯文的語句我仿寫出了:select CONCAT('
update ams_order set detectioncity=',detectioncity,' where carid=',carid,';' ) from ams_car where overTime='2018-03-25 14:30:00' and flag=1 and carid in (select carid from ams_order where overTime='2018-03-25 14:30:00' and flag=1 and detectionCity=0);所有的欄位都用逗號分隔,如果是字串,要加引號。根據搜尋出的欄位值批量執行就好了。怕出問題,寫了前幾個車編對照了下。select CONCAT('update ams_order set detectioncity=',detectioncity,' where carid=',carid,';' ) from ams_car where overTime='2018-03-25 14:30:00' and flag=1 and carid in (6585565,6585779,6584743,1410868,5913933,6173107,6180839,6261201);對照城市id,證實上面的語句是正確的。於是執行。-------------------------------------------------------------------------------------------------------------
網上百度還找到另一種方法。還沒試驗。updateinnerjoinona.bid=b.id seta.x=b.x,a.y=b.y ;測試:mysql> use xuning;No connection. Trying to reconnect...Connection id: 5569Current database: *** NONE ***Reading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changedmysql> create table co (id int primary key auto_increment,name char(10));
Query OK, 0 rows affected (0.03 sec)mysql> desc co;+-------+----------+------+-----+---------+----------------+| Field | Type | Null | Key | Default | Extra |+-------+----------+------+-----+---------+----------------+| id | int(11) | NO | PRI | NULL | auto_increment || name | char(10) | YES | | NULL | |+-------+----------+------+-----+---------+----------------+2 rows in set (0.00 sec)mysql> create table py like co;Query OK, 0 rows affected (0.04 sec)mysql> desc py;+-------+----------+------+-----+---------+----------------+| Field | Type | Null | Key | Default | Extra |+-------+----------+------+-----+---------+----------------+| id | int(11) | NO | PRI | NULL | auto_increment || name | char(10) | YES | | NULL | |+-------+----------+------+-----+---------+----------------+2 rows in set (0.01 sec)mysql> insert co values(null,'one'),(null,'one'),(null,'one'),(null,'one'),(null,'one'),(null,'one'),(null,'one'),(null,'one'),(null,'one');Query OK, 9 rows affected (0.01 sec)Records: 9 Duplicates: 0 Warnings: 0mysql> select * from co;+----+------+| id | name |+----+------+| 1 | one || 2 | one || 3 | one || 4 | one || 5 | one || 6 | one || 7 | one || 8 | one || 9 | one |+----+------+9 rows in set (0.00 sec)mysql> insert co values(null,'one');Query OK, 1 row affected (0.02 sec)mysql> insert py values(null,null);Query OK, 1 row affected (0.01 sec)mysql> insert py values(null,null),(null,null),(null,null),(null,null),(null,null),(null,null),(null,null),(null,null),(null,null),(null,null),(null,null);Query OK, 11 rows affected (0.02 sec)Records: 11 Duplicates: 0 Warnings: 0mysql> select * from py;+----+------+| id | name |+----+------+| 1 | NULL || 2 | NULL || 3 | NULL || 4 | NULL || 5 | NULL || 6 | NULL || 7 | NULL || 8 | NULL || 9 | NULL || 10 | NULL || 11 | NULL || 12 | NULL |+----+------+12 rows in set (0.00 sec)mysql> delete from py where id>10;Query OK, 2 rows affected (0.01 sec)mysql> commit;Query OK, 0 rows affected (0.00 sec)mysql> update py inner join co on py.id=co.id set py.name=co.name;Query OK, 10 rows affected (0.02 sec)Rows matched: 10 Changed: 10 Warnings: 0mysql> select * from py;+----+------+| id | name |+----+------+| 1 | one || 2 | one || 3 | one || 4 | one || 5 | one || 6 | one || 7 | one || 8 | one || 9 | one || 10 | one |+----+------+10 rows in set (0.01 sec)mysql> 發現第二個方法比第一個好用。