mysql - SQL coalesce(): what type does the combined column have? -
lets use coalesce() combine 2 columns 1 in select , subsequently view constructed around such select.
tables:
values_int id integer(11) primary key value integer(11) values_varchar id integer(11) primary key value varchar(255) vals id integer(11) primary key value integer(11) //foreign key both values_int , values_varchar the primary keys between values_int , values_varchar unique , allows me do:
select vals.id, coalesce(values_int.value, values_varchar.value) value vals join values_int on values_int.id = vals.value join values_varchar on values_varchar.id = vals.value this produces nice assembled view id column , combined value column contains actual values 2 other tables combined single column.
- what type combined column have?
- when turned view , queried clause using combined "value" column, how handled type-wise? i.e.
where value > 10
som rambling thoughts in need (most wrong): reason asking alternative design have 3 tables merged 1 int values in 1 column , varchar in other. of course produce lots of null values in both columns saved me joins. reason not solution because require additional type checking choose right column , deal null values maybe presented design require same (if resulting column varchar). hope passes clause down view source (so column not have type per se) wrong that.
you query should explicit clear, in case mysql using varchar.
i write query clear
coalesce(values_int.value,cast(values_varchar.value integer), 0) or
coalesce(cast(values_int.value varchar(20)),values_varchar.value,'0') you should put in last value unless want column null if both columns null.
Comments
Post a Comment