How can I merge two tables into one table ?
For example, let say I have table named person with first_name and last_name,gender then I have another table called person_d and this has first_name, last_name, telephone and email.
The new table should contain first_name, last_name, telephone and email,gender
but i want to do that in an automatic way without even know what the fields name are just by using a conditions
Is that possible with sql or i have to do it using php? I thought that I can create php script and get fields name into two arrays then compare for doubles and add them to new array and delete them after that we will have arrays with no doubles this is my code
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "mydb";
//mysql and db connection
$db = new mysqli($servername, $username, $password, $dbname);
if ($db->connect_error) { //error check
die("Connection failed: " . $con->connect_error);
} else {
}
$query1 = $db->query("SELECT * FROM tablename1");
$query2 = $db->query("SELECT * FROM tablename2");
$table1 = array();
$table2 = array();
$newtable = array();
while ($property = mysqli_fetch_field($query)) { //fetch table field name
array_push($table1,$property->name);
}
while ($property = mysqli_fetch_field($query)) { //fetch table field name
array_push($table2,$property->name);
}
foreach($table1 as $field1) {
foreach($table2 as $field2) {
if ($field1==$field2)
{
array_push($newtable,$field1);
unset($table1[$field1]);
unset($table1[$field2]);
}
}
}
and after that i create new table with fields from arrays side by side
Is this the best way or there is another way to do that ?