Manoj K
Manoj K

Reputation: 477

merge two tables with different columns mysql

I want to merge two tables with different columns mysql. For e.g.

Table 1 :

-------------------------------------------------------------
item_id   title   slug   type   views   updatedAt   createdAt
-------------------------------------------------------------
 1    sometitle someslg  1     43454    timestamp   timestamp
 2    sometitle someslg  1     43566    timestamp   timestamp

Table 2:

-------------------------------------------------------------
id    ptitle   slug     pviews   updatedAt   createdAt
-------------------------------------------------------------
 1  sometitle  someslg  3434    timestamp   timestamp
 2  sometitle  someslg  6454    timestamp   timestamp
 3  sometitle  someslg  5454    timestamp   timestamp

The above tables are examples. I have merged with UNION. Here is my query

SELECT * ((SELECT t1.item_id,t1.title,t1.slug,t1.type,t1.views,t1.updatedAt,t1.createdAt 
FROM table1
t1) UNION ALL (SELECT t2.id,t2.ptitle,t2.slug,'',t2.pviews,t2.updatedAt,t2.createdAt)) t3 
ORDER BY t3.item_id ASC

This is working fine After Executing the query i will get the results like

-------------------------------------------------------------
item_id   title   slug   type   views   updatedAt   createdAt
-------------------------------------------------------------
 1    sometitle someslg  1     43454    timestamp   timestamp
 1    sometitle someslg        3434     timestamp   timestamp
 2    sometitle someslg  1     43566    timestamp   timestamp
 2    sometitle someslg        6454     timestamp   timestamp
 3    sometitle someslg        5454     timestamp   timestamp

But i want a virtual column in the result that shows the row from which table as shown below

--------------------------------------------------------------------------
item_id   title   slug   type   views   updatedAt   createdAt   from_tbl
--------------------------------------------------------------------------
 1    sometitle someslg  1     43454    timestamp   timestamp   t1
 1    sometitle someslg        3434     timestamp   timestamp   t2
 2    sometitle someslg  1     43566    timestamp   timestamp   t1
 2    sometitle someslg        6454     timestamp   timestamp   t2
 3    sometitle someslg        5454     timestamp   timestamp   t2

Upvotes: 2

Views: 12185

Answers (4)

Nady Shalaby
Nady Shalaby

Reputation: 644

This SQL script can achieve the same purpose without carrying out the headache of specifying the whole list of column names. This script can guess the match column names and remove duplicates on insertion process.

SET @source_table = '`users_old`.`url`';
SET @target_table = '`users_new`.`url`';

SELECT GROUP_CONCAT(column_name)
 FROM INFORMATION_SCHEMA.COLUMNS a 
 WHERE a.TABLE_NAME = 'url'
 AND a.TABLE_SCHEMA = 'users_old'
 AND a.COLUMN_NAME IN (
   SELECT b.COLUMN_NAME
   FROM INFORMATION_SCHEMA.COLUMNS b 
   WHERE b.TABLE_NAME = 'url'
   AND b.TABLE_SCHEMA = 'users_new' 
 ) INTO @columns;

SET @s = CONCAT('REPLACE INTO ', @target_table , ' (', @columns ,') ' , ' SELECT ', @columns ,' FROM ', @target_table ,' UNION ALL SELECT ', @columns , ' FROM ', @source_table);

PREPARE stmt FROM @s;
EXECUTE stmt;

Upvotes: 0

Attapol T.
Attapol T.

Reputation: 1

CREATE TABLE table3 AS ( select t1.*,t2.* from table1 as t1, table2 as t2 where t1.LinkID1 = t2.LinkID2 ); INSERT INTO table3

** LinkID1 and LinkID2 are index referance each together

Upvotes: 0

Pramod
Pramod

Reputation: 96

try this way

SELECT t3.*
FROM ((SELECT t1.item_id, t1.title, t1.slug, t1.type, t1.views, t1.updatedAt, t1.createdAt, '1' as from_tbl
       FROM table1 t1
      ) UNION ALL
      (SELECT t2.id, t2.ptitle, t2.slug, '', t2.pviews, t2.updatedAt, t2.createdAt, '2'  FROM table2 t2
      )
     ) t3
ORDER BY t3.item_id ASC

Upvotes: 1

Gordon Linoff
Gordon Linoff

Reputation: 1269563

Just add the column in:

SELECT t3.*
FROM ((SELECT t1.item_id, t1.title, t1.slug, t1.type, t1.views, t1.updatedAt, t1.createdAt, 't1' as from_tbl
       FROM table1 t1
      ) UNION ALL
      (SELECT t2.id, t2.ptitle, t2.slug, '', t2.pviews, t2.updatedAt, t2.createdAt, 't2'
      )
     ) t3
ORDER BY t3.item_id ASC

Upvotes: 10

Related Questions