简体   繁体   中英

Joining multiple tables in a SQL dataset based on their names?

I have a database in SQL Server which contains huge numbers of tables. But each table has 'id' and 'value'. I want to join all the tables which their names contain a certain text (eg, 'ts2') based on their id (id is a common key). So my desired table should have 'id' and the 'values' of each table with the name of the table. For example:

 TableAts2: 
          id,
          value

 TableBts2:
          id,
          value

 Tablts2C:
         id,
         value
         ...

My desired table:

 mytable:
         id, value_TableAts2,value_TableBts2, value_Tablts2C

Some source datatables

CREATE TABLE TableAts2 (id INT, value INT); INSERT INTO TableAts2 VALUES (1,1), (2,2); CREATE TABLE TableBts2 LIKE TableAts2; INSERT INTO TableBts2 VALUES (1,11), (3,33); CREATE TABLE TableCts2 LIKE TableAts2; INSERT INTO TableCts2 VALUES (2,222), (3,333);

Build the query text

SELECT CONCAT( 'SELECT id, ', GROUP_CONCAT(table_name, '.value value_',table_name), '\nFROM (', GROUP_CONCAT('SELECT id FROM ',table_name SEPARATOR ' UNION '), ') ids\nLEFT JOIN ', GROUP_CONCAT(table_name, ' USING (id)' SEPARATOR '\nLEFT JOIN ') ) INTO @sql FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = DATABASE();

Check the query built

SELECT @sql;
 SELECT id, TableAts2.value value_TableAts2,TableBts2.value value_TableBts2,TableCts2.value value_TableCts2 FROM (SELECT id FROM TableAts2 UNION SELECT id FROM TableBts2 UNION SELECT id FROM TableCts2) ids LEFT JOIN TableAts2 USING (id) LEFT JOIN TableBts2 USING (id) LEFT JOIN TableCts2 USING (id)

Execute the query

PREPARE stmt FROM @sql; EXECUTE stmt; DROP PREPARE stmt;
id value_TableAts2 value_TableBts2 value_TableCts2
1 1 11 null
2 2 null 222
3 null 33 333

db<>fiddle here

Of course the query needs in a check that the table contains both id and value columns (by according subquery to INFORMATION_SCHEMA.COLUMNS).

First, you need to insert one column in each table. For example TableAts2: id, user_id, value

TableBts2: id, user_id, value

Tablts2C: id, user_id, value...

Second, you need to join each table with user_id. Now please run this sql command:

select t.value_TableAts2 as value_TableAts2, t.value_TableBts2 as value_TableBts2, Tablts2C.value as value_Tablts2C from (select TableAts2.value as value_TableAts2, TableBts2.value as value_TableBts2, TableAts2.user_id as user_id from TableAts2 left join TableBts2 on TableAts2.user_id = TableBts2.user_id) as t left join Tablts2C on t.user_id = Tablts2C.user_id

Result::: Your desired table: id, value_TableAts2,value_TableBts2, value_Tablts2C

i think this should work fine for you:

select tableAts1.* , TableBts2.* , Tablts2C.*
from tableAts1
inner join TableBts2 on tableAts.id = TableBts2.id
inner join Tablts2C on tableBts2.id = tablts2C.id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM