Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining two tables with a different column

I have to select requests that i want to combine using UNION :

Table 1 : which is a join between Table_a, table_b and table_c

id_table_a   desc_table_a   table_b.id_user  table_c.field
-----------------------------------------------------------
1            desc1            1                 field1
2            desc2            2                 field2
3            desc3            3                 field3

Table 2 : which is also a join between Table_a, table_b and table_c but it has these columns:

id_table_a   desc_table_a   table_c.id_user  table_c.field
-----------------------------------------------------------
4            desc4            4                 field4
5            desc5            5                 field8
9            desc9            6                 field9

the difference between the two is that in Table1 we have table_b.id_user and table two table_c.id_user instead .

Combined Table

  id_table_a   desc_table_a     id_user  table_c.field
    -----------------------------------------------------------
    1            desc1            1                 field1
    2            desc2            2                 field2
    3            desc3            3                 field3
    4            desc4            4                 field4
    5            desc5            5                 field5
    9            desc9            6                 field6

I already have the join requests working but doing union between the two gives me

ORA-01790 expression must have same datatype as corresponding expression

which make sense because the two columns are not the same .

Im using zend_Db's join and union for this .

So how can i tackle this to get the result ?

Thanks.

like image 541
Mouna Cheikhna Avatar asked Mar 23 '26 22:03

Mouna Cheikhna


1 Answers

Are the results above the same as the sequence of columns in your table? because oracle is strict in column orders. this example below produces an error:

create table test1_1790 (
col_a varchar2(30),
col_b number,
col_c date);

create table test2_1790 (
col_a varchar2(30),
col_c date,
col_b number);

select * from test1_1790
union all
select * from test2_1790;

ORA-01790: expression must have same datatype as corresponding expression

As you see the root cause of the error is in the mismatching column ordering that is implied by the use of * as column list specifier. This type of errors can be easily avoided by entering the column list explicitly:

select col_a, col_b, col_c from test1_1790
union all
select col_a, col_b, col_c from test2_1790;

A more frequent scenario for this error is when you inadvertently swap (or shift) two or more columns in the SELECT list:

select col_a, col_b, col_c from test1_1790
union all
select col_a, col_c, col_b from test2_1790;

OR if the above does not solve your problem, how about creating an ALIAS in the columns like this: (the query is not the same as yours but the point here is how to add alias in the column.)

SELECT id_table_a, 
       desc_table_a, 
       table_b.id_user as iUserID, 
       table_c.field as iField
UNION
SELECT id_table_a, 
       desc_table_a, 
       table_c.id_user as iUserID, 
       table_c.field as iField

hope this helps.

like image 54
John Woo Avatar answered Mar 26 '26 14:03

John Woo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!