views:

28

answers:

1

Hi,

I have a datareader that binds a sql select (with 10 columns from table1) , i want to append another with 5 different cols from table2 to this first sql select, i can't do UNION as it has different number of columns, (one query has 10, another returns 5 cols).

Is there any other way of implementing this, via mysql?

Also i need to append the additional 5 columns based on a condition, is there via mysql select query to write with using if then in the select query?

Thanks.

A: 

What about unioning with nulls? The query below will remove duplicates between the two datasets.

SELECT col1,col2,col3,col4
FROM table1
UNION ALL
SELECT col1,col2,null,null
FROM table2

If you want to remove duplicates inside of each dataset use the query below:

SELECT DISTINCT col1,col2,col3,col4
FROM table1
UNION ALL
SELECT DISTINCT col1,col2,null,null
FROM table2

You can use anything you want for the default value after col1 and col2.

null, 'none', '' all should work.

Abe Miessler
Remember that you probably want UNION ALL though, not UNION
nos
Wouldn't the results be the same because of the nulls in the second query?
Abe Miessler
Am getting duplicates in the result while i do union?
Sharpeye500
Thanks Abe, is there any way to remove the duplicate and show the entry value without null?
Sharpeye500
You can remove duplicates between the two datasets by using UNION ALL instead of UNION. What do you mean by "show the entry value without null"? I've updated the query to use UNION ALL
Abe Miessler
I have this condition in mysql querySELECT col1,col2,col3,col4 FROM table1 -> this I select produces 5 rows (R1,R2,R3,R4,R5) where (C1,c2,c3 are null) UNION SELECT col1,col2,null,null FROM table2; -> this II select produces 2 rows (R4,R5) where (C1,c2,c3 are not null)Is there any way to remove the duplicate and show the entry value without null?I want to send the query result with total of 5 rows with values for last 2 rows instead of total 7 rows.
Sharpeye500
Ok i got the answer on I select i added a not exists which checks data not in another table, then i combine with the II table.SELECT col1,col2,col3,col4FROM table1 and NOT EXISTS (select <primary_key_col> from table3 where table1.ID=table3.ID)UNION SELECT col1,col2,null,nullFROM table2Thanks.
Sharpeye500