2017-02-12 113 views
0

我有一个有requestID列的Mysql表。 requestId是String类型。我有三种requestID。比方说bed,tvfridge基于MYSQL表中列的值创建一个新的列

  • 对于bed请求的类型,在requestID中没有任何内容。例如abc123
  • 对于tvfridge请求的类型,requestId分别附加tvfridge。 e.g abc123.tvabc123.fridge

我需要建立一个具有有记入bedtvfridge所有列加列requestType的新表。

输入

| ID | RequestId  | 
| 1 | abc123  | 
| 2 | abc123.tv  | 
| 3 | abc123.fridge | 

输出

| ID | RequestId  | RequestType |  
| 1 | abc123  | bed  | 
| 2 | abc123.tv  | tv   | 
| 3 | abc123.fridge | fridge  | 
+0

你能显示一些样本数据和预期的输出吗?目前还不清楚你需要什么。 –

回答

1

可以使用SUBSTRING_INDEX字符串中.后得到的RequestType列的字符。然后使用CREATE TABLE AS..SELECT..创建一个包含所有现有列和新计算列的新表。

create table newtable as 
select e.*, 
case when substring_index(requestID,'.',-1)=requestID then 'bed' 
else substring_index(requestID,'.',-1) end as requestType 
from existing_table e 
相关问题