2013-12-11 20 views
0

在選擇查詢中,我可以組成列,但是如何爲其分配值?如何爲選擇查詢中的列指定值

select a.col1, a.col2, 'column3' 
from A a 
    union 
select b.col2, b.col3, b.col3 as `column3` 
from B b 

我想的-1一個缺省值分配給column3列我在第一個查詢作出。另外,我希望列的標題仍爲column3。這可能嗎?

+2

選擇a.col1,a.col2,' - 1' –

回答

0

試試這個

select a.col1, a.col2, -1 as column3 
from A a 
    union 
select b.col2, b.col3, b.col3 
from B b 

或者這是否b.col3爲varchar

select a.col1, a.col2, '-1' column3 
from A a 
    union 
select b.col2, b.col3, b.col3 
from B b 

如果A和B表有樹列的數據庫會做一個DISTINCT以避免相同的值,如果你願意,使用UNION ALL

+0

這樣做,'-1',也成爲列標題。這不是我期望的效果。我仍然希望列名是'column3',但默認值是'-1'。 – birdy

+0

根據您的要求編輯我的答案。 –

0
To create a dummy column and assgin values to it we can use 

select a.col1, a.col2, '-1' as col3 from A a 
0

你最那裏的方式:

select a.col1, a.col2, -1 as 'column3' 
from A a 
    union 
select b.col2, b.col3, b.col3 
from B b