当前位置: 动力学知识库 > 问答 > 编程问答 >

mysql - Trying to combine multiple database tables into one table

问题描述:

For some reason combining multiple tables using the following code does not work. I want to also remove the duplicates, so I assume UNION is correct to use and not to use UNION ALL.

How would I combine all three database tables from database1, database2, and database3 into a single table within database4?

All databases tables have identical structures.

<?php

// Variable definitions omitted

// Connect to all 4 Databases

$connection1 = mysql_connect($host1, $username1, $password1) or die ('Cannot connect to the database because: ' . mysql_error());

$connection2 = mysql_connect($host2, $username2, $password2) or die ('Cannot connect to the database because: ' . mysql_error());

$connection3 = mysql_connect($host3, $username3, $password3) or die ('Cannot connect to the database because: ' . mysql_error());

$connection4 = mysql_connect($host4, $username4, $password4) or die ('Cannot connect to the database because: ' . mysql_error());

// Merge all 4 Databases into the Search Database 4

mysql_select_db ($database4);

mysql_query("

SELECT * FROM $database4.$TableName4

UNION

SELECT * FROM $database1.$TableName1

UNION

SELECT * FROM $database2.$TableName2

UNION

SELECT * FROM $database3.$TableName3

");

// Close databases connections

mysql_close($connection1);

mysql_close($connection2);

mysql_close($connection3);

mysql_close($connection4);

?>

Thanks in advance.

分享给朋友:
您可能感兴趣的文章:
随机阅读: