In a select query I can make-up columns but how can I assign values for them?
Example
select a.col1, a.col2, 'column3'
from A a
union
select b.col2, b.col3, b.col3 as `column3`
from B b
I want to assign a default value of -1
to the column3
column I made in the first query. Also, I want the title of the column to still be column3
. is this possible?
Try this
select a.col1, a.col2, -1 as column3
from A a
union
select b.col2, b.col3, b.col3
from B b
Or this if b.col3 is varchar
select a.col1, a.col2, '-1' column3
from A a
union
select b.col2, b.col3, b.col3
from B b
If A and B table has the same values for the tree columns the database will do an DISTINCT to avoid that, if you want, use UNION ALL
To create a dummy column and assgin values to it we can use
select a.col1, a.col2, '-1' as col3 from A a
You are most of the way there:
select a.col1, a.col2, -1 as 'column3'
from A a
union
select b.col2, b.col3, b.col3
from B b
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.