If you don't need the id for any reason you can create a temporary table and insert both tables into it.
I create 2 tables one named table1 and one named table2 with the following code:
CREATE TABLE table (
ID int(10) unsigned NOT NULL AUTO_INCREMENT,
column1 int(10) unsigned NOT NULL,
column2 int(10) unsigned NOT NULL,
PRIMARY KEY (ID)
);
- CREATE TABLE table (
- ID int(10) unsigned NOT NULL AUTO_INCREMENT,
- column1 int(10) unsigned NOT NULL,
- column2 int(10) unsigned NOT NULL,
- PRIMARY KEY (ID)
- );
Then in PHP/SQL I create a temporary table and import both tables into it without the id field. After that you can just select from the temporary table which contains data from both tables.
$DBLink = mysql_connect('localhost', 'root', '');
mysql_select_db('test_db');
/* create temporary table */
$query = "CREATE TEMPORARY TABLE temporary_table ( column1 int(10) unsigned NOT NULL, column2 int(10) unsigned NOT NULL)";
mysql_query($query);
/* insert table1 into temporary table */
$query = "INSERT INTO combined_table (column1, column2) SELECT column1, column2 FROM table1";
mysql_query($query);
/* insert table2 into temporary table */
$query = "INSERT INTO temporary_table (column1, column2) SELECT column1, column2 FROM table2";
mysql_query($query);
/* select your data from temporary table */
$query = "SELECT * FROM temporary_table";
$result = mysql_query($query);
if (is_resource($result)) {
while ($row = mysql_fetch_array($result)) {
echo $row['column1'].', '.$row['column2'].'<br/>';
}
}
- $DBLink = mysql_connect('localhost', 'root', '');
- mysql_select_db('test_db');
- /* create temporary table */
- $query = "CREATE TEMPORARY TABLE temporary_table ( column1 int(10) unsigned NOT NULL, column2 int(10) unsigned NOT NULL)";
- mysql_query($query);
- /* insert table1 into temporary table */
- $query = "INSERT INTO combined_table (column1, column2) SELECT column1, column2 FROM table1";
- mysql_query($query);
- /* insert table2 into temporary table */
- $query = "INSERT INTO temporary_table (column1, column2) SELECT column1, column2 FROM table2";
- mysql_query($query);
- /* select your data from temporary table */
- $query = "SELECT * FROM temporary_table";
- $result = mysql_query($query);
- if (is_resource($result)) {
- while ($row = mysql_fetch_array($result)) {
- echo $row['column1'].', '.$row['column2'].'<br/>';
- }
- }
Probably a slow way to do it but it may solve the problem.