Mysql - union of 2 tables with different columns - mysql

I know the topic is already on other threads, but my problem is that i could not use union (table 1 has 60 columns; table 2 has only 7). Is there another way than creating for table 2 ...53 empty columns?
Is it possible to generate the result in one query?
Thank you!

You can simply do this by replacing non existent columns with nulls like below
Select Col1, Col2, Col3, Col4, Col5 from Table1
Union
Select Col1, Col2, Col3, Null as Col4, Null as Col5 from Table2
Replace the columns in tables with null if the column does not exist.

It is possible, since you can add any number of arbitrary columns in a select:
select field1, field2, field3 from table1
union
select field4, null, field5 from table2
In the above example I used a constant null value as the 2nd field, but you can choise any value befitting the data type of the existing column in the other table.

Related

Is MySQL handling null values correctly when count(distinct col1, col2, col3)

I was having trouble determining a possible unique key in a poorly defined table. The table had 5000 rows. I selected distinct on the fields I thought might be a unique key.
select count(distinct col1, col2)
from tab1;
The result was 4980 records. Then I checked the 20 records and found that the values for col2 where null, but adding col3 should give me uniqueness.
select count(distinct col1, col2, col3)
from tab1;
The result was still 4980. What the? So I changed the query to this.
select col1, col2, col3, count(*)
from tab1
group by col1, col2, col3
having count(*) > 1;
With this I got zero rows, so col1, col2, and col3 are unique. So what was wrong with the first three column query? I tried this.
select count(distinct col1, coalesce(col2, ''), col3)
from tab1;
This returned 5000 records.
It is likely that the multiple fields are being concatenated together in one field in the engine, and concatenating col1, NULL, col3 is resulting in NULL and that is why it is acting this way. But, the result seems to break the NULL standards that MySQL seems to want to follow. Is this a MySQL bug?
The manual specifically says that COUNT(DISTINCT expr [,expr...])
Returns a count of the number of rows with different non-NULL expr values.
which is the behaviour you are seeing.

Merge several rows to own row by value of one column in MySQL

i have a table like this one:
There are some rows with the same value in col2 and e.g. for every 't1' is only one value in col3 (and so on), no duplicates.
and I want to have a query for this result:
with
SELECT * FROM table GROUP BY col2
i get no all values for t1 e.g..
You can pick min/max value per group as
SELECT
max(col1) col1,
col2,
max(col3) col3,
max(col4) col4,
max(col5) col5,
max(col6) col6,
max(col7) col7
FROM table
GROUP BY col2

How to merge two tables without id and with different number of columns in MySQL

I have two tables with different columns. Tables doesn't have id column. They have the same number of rows. I want to merge them in new table. I've tried to do this like this:
CREATE TABLE test_3_cut_dest as
SELECT * FROM test_3_cut_
UNION
SELECT * FROM test_3_cut
but got error:
each UNION query must have the same number of columns
I want to know how achieve merging of two tables with different number of columns without specifying list of columns?
I don't think it works with select * when it comes to not knowing how many columns each table has. The best way to do it is like this:
SELECT A, B, C, D, E, F, G, H FROM test_3_cut_
UNION
SELECT A, B, NULL AS C, NULL AS D, NULL AS E, NULL AS ... FROM test_3_cut
I've done it like this considering test_3_cut has fewer columns than test_3_cut_
Union requires that all participating selects yield matching record sets, meaning same number of columns and compatible data types for each column.
As for your question, you can use placeholders wherever there is a mismatch. For instance:
SELECT Col1, Col2, 'DUMMYVALUE'
FROM Table1
UNION ALL
SELECT Col1, Col2, Col3
FROM Table2 ;
Now, if table1 has 3 textual columns and table2 has 6 textual columns, you can go with:
SELECT * , 'DUMMYVALUE1','DUMMYVALUE2','DUMMYVALUE3'
FROM Table1
UNION ALL
SELECT *
FROM Table2 ;
Union works like this:
Select column1 from table1
union
select column1 from table2
Number of columns must be same in both Select Queries.
You have to select from the two tables the same set of columns. If one of the tables has more columns, you can either select only the columns they share or select a fake value for the missing column from the table that has less.
Example
Table1
col1 | col2 | col3
Table2
col1 | col2
You can do this
select col1, col2 from Table1
union all
select col1, col2 from Table2
or this
select col1, col2, col3 from Table1
union all
select col1, col2, '' from Table2

MYSQL: Insert one value from another table while populating other columns regularly

Lets say we have a table (1):
id | col1 | col2
And another table (2):
id | col3
Task is to insert all col3 distinct values to col1 at the same time populating col2 with random integer value
A couple of solutions here.
This uses a sub query to return the distinct values of col2.
INSERT INTO table1 (id, col1, col2)
SELECT NULL, col2, FLOOR(RAND()*(1000))+1
FROM
(
SELECT DISTINCT col2
FROM table2
)
The following abuses the GROUP BY clause to only generate rows for distinct values of col2. While this should be OK on a default install of MySQL, it might not work depending on the options set up for your installation and also probably wouldn't work in other flavours of SQL.
INSERT INTO table1 (id, col1, col2)
SELECT NULL, col2, FLOOR(RAND()*(1000))+1
FROM table2
GROUP BY col2

"Merging" columns from several tables

Using SQL Server 2008, suppose I have several tables with 3 common columns (not related):
TABLE1
col1 colSomeOther col2 colAnotherOne
TABLE2
col1 colSomeOther col2 colAnotherOne
TABLE3
col1 colSomeOther col2 colAnotherOne
I would like to create a view which merges col1 and 2 for the 3 tables above. Something like:
VIEW
col1 col2
where col1 contains ALL elements from table 1, 2 and 3, and col2 contains ALL elements from col2 in table 1, 2, 3.
Is this possible?
Yep. This is a "union"; multiple result sets of the same "signature" (number and type of data columns), concatenated one after the other. The query to do this is as simple as:
SELECT col1, col2 FROM TABLE1
UNION ALL
SELECT col1, col2 FROM TABLE2
UNION ALL
SELECT col1, col2 FROM TABLE3
If you want the query to "de-duplicate" the results, returning only unique rows, omit the "ALL" keywords from the unions. With the ALL keywords, it simply tacks on the results of each SELECT to the combined result set, including rows from Table2 that may have exactly the same data as Table1.
I think you are asking for an UNION:
select col1, col2 from table1
UNION ALL
select col1, col2 from table2
UNION ALL
select col1, col2 from table3
Should work as long as col1 and col2 have compatible data types across all three tables.
If you want to eliminate duplicate rows then use UNION instead of UNION ALL.