1. 程式人生 > >使用concat做字符串拼接和數據遷移

使用concat做字符串拼接和數據遷移

字符串拼接 arc employee num emp 數據遷移 class dup fault

作用:

  解決一開始數據庫建立不合理造成的字段冗余,從而提取部分字段,數據遷移、拼接字符串的功能。

格式:

  concat(字段1,‘間隔符‘,字段2....)

  concat_ws(‘間隔符‘,字段1,字段2)

數據準備:

  

#創建表
create table employee(
id int not null unique auto_increment,
name varchar(20) not null,
sex enum(male,female) not null default male, #大部分是男的
age int(3) unsigned not null default 28,
hire_date date 
not null, post varchar(50), post_comment varchar(100), salary double(15,2), office int, #一個部門一個屋子 depart_id int ); #插入記錄 #三個部門:教學,銷售,運營 insert into employee(name,sex,age,hire_date,post,salary,office,depart_id) values (egon,male,18,20170301,老男孩駐沙河辦事處外交大使,7300.33,401,1), #以下是教學部 (alex,male,78,20150302
,teacher,1000000.31,401,1), (wupeiqi,male,81,20130305,teacher,8300,401,1), (yuanhao,male,73,20140701,teacher,3500,401,1), (liwenzhou,male,28,20121101,teacher,2100,401,1), (jingliyang,female,18,20110211,teacher,9000,401,1), (jinxin,male,18,19000301,teacher,30000,401,1), (成龍,male,48,20101111,
teacher,10000,401,1), (歪歪,female,48,20150311,sale,3000.13,402,2),#以下是銷售部門 (丫丫,female,38,20101101,sale,2000.35,402,2), (丁丁,female,18,20110312,sale,1000.37,402,2), (星星,female,18,20160513,sale,3000.29,402,2), (格格,female,28,20170127,sale,4000.33,402,2), (張野,male,28,20160311,operation,10000.13,403,3), #以下是運營部門 (程咬金,male,18,19970312,operation,20000,403,3), (程咬銀,female,18,20130311,operation,19000,403,3), (程咬銅,male,18,20150411,operation,18000,403,3), (程咬鐵,female,18,20140512,operation,17000,403,3) ;

  操作示例:

  只做字符串的拼接:

 1 mysql> select concat(name:,name) as mingzi,concat(sex:,sex) from  emp;
 2 +-----------------+--------------------+
 3 | mingzi          | concat(sex:,sex) |
 4 +-----------------+--------------------+
 5 | name:egon       | sex:male           |
 6 | name:alex       | sex:male           |
 7 | name:wupeiqi    | sex:male           |
 8 | name:yuanhao    | sex:male           |
 9 | name:liwenzhou  | sex:male           |
10 | name:jingliyang | sex:female         |
11 | name:jinxin     | sex:male           |
12 | name:成龍       | sex:male           |
13 | name:歪歪       | sex:female         |
14 | name:丫丫       | sex:female         |
15 | name:丁丁       | sex:female         |
16 | name:星星       | sex:female         |
17 | name:格格       | sex:female         |
18 | name:張野       | sex:male           |
19 | name:程咬金     | sex:male           |
20 | name:程咬銀     | sex:female         |
21 | name:程咬銅     | sex:male           |
22 | name:程咬鐵     | sex:female         |
23 +-----------------+--------------------+
24 18 rows in set (0.00 sec)

  數據遷移(同時進行字符串拼接):

 1 mysql> create table aa select concat(name:,name) as mingzi,concat(sex:,sex) from  emp;
 2 Query OK, 18 rows affected (0.03 sec)
 3 Records: 18  Duplicates: 0  Warnings: 0
 4 
 5 mysql>
 6 mysql> show tables;
 7 +---------------+
 8 | Tables_in_db2 |
 9 +---------------+
10 | aa            |
11 | big_data      |
12 | emp           |
13 +---------------+
14 3 rows in set (0.00 sec)
15 
16 mysql> select * from aa ;
17 +-----------------+--------------------+
18 | mingzi          | concat(sex:,sex) |
19 +-----------------+--------------------+
20 | name:egon       | sex:male           |
21 | name:alex       | sex:male           |
22 | name:wupeiqi    | sex:male           |
23 | name:yuanhao    | sex:male           |
24 | name:liwenzhou  | sex:male           |
25 | name:jingliyang | sex:female         |
26 | name:jinxin     | sex:male           |
27 | name:成龍       | sex:male           |
28 | name:歪歪       | sex:female         |
29 | name:丫丫       | sex:female         |
30 | name:丁丁       | sex:female         |
31 | name:星星       | sex:female         |
32 | name:格格       | sex:female         |
33 | name:張野       | sex:male           |
34 | name:程咬金     | sex:male           |
35 | name:程咬銀     | sex:female         |
36 | name:程咬銅     | sex:male           |
37 | name:程咬鐵     | sex:female         |
38 +-----------------+--------------------+
39 18 rows in set (0.00 sec)

使用concat做字符串拼接和數據遷移