2015年1月6日 星期二

MySQL 欄位合併與字串合併


將兩個資料表的某一欄位合併輸出成一個欄位

table_1
t1_name
a
b
c
d

table_2
t2_name
e
f
g


newName
a
b
c
d
e
f
g 

語法
SELECT t1_name as newName FROM table_1 2 union all SELECT t2_name FROM table_2 order by newName 

如果是單一資料表的兩個欄位要合併的話
語法如下

Select distinct LarCode as NewColumn From prodclass Where LarSeq = (Select LarSeq From prodclass Where ClassId = 1) union all Select concat(concat(MidSeq,'.'),MidCode) From prodclass Where LarSeq = (Select LarSeq From prodclass Where ClassId = 1)

concat(str1,str2) 字串合併
union all 欄位合併

沒有留言:

張貼留言