Link to home
Create AccountLog in
Avatar of Zado
ZadoFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Display two mysql tables as one with php

Hi,

I have two tables in my database, one contains fixed data called 'catalogue data', second one is for updates. What I want to do is display combination of those two tables as one table, is this possible? Both tables has same column names.

Thanks for any help.
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Yes, it's possible.  In your query you can assign names to the results set.

The query looks like this:

SELECT table_name.column_name as result_name FROM table_name...

That will help you avoid name collisions, and the results set can contain as many columns as you might need from the two tables.
ASKER CERTIFIED SOLUTION
Avatar of CRJ2000
CRJ2000
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Hi,

I am not sure I understand what you mean, but if your problem is like this:
Table 1
----------
A   B     C
1   5     6
5   7     9

Table 2
----------
A   B     C
4   9     4
7   8     1

Output
----------
A   B     C
1   5     6
5   7     9
4   9     4
7   8     1

Open in new window


then the simplest you can do is have two SELECT statements.

Or would you like to combine the data (like Descartes multiplication)? Then you need a JOIN statement. In this case, please give some more details, and possibly an example.

gsx1022

You need to use Union all sql
Avatar of Zado

ASKER

I want to do exactly what you wrote, display all the records from both tables. Is this proper syntax for union?
SELECT field1, field2, field3 FROM table1 UNION ALL SELECT field1, field2, field3 FROM table2

Open in new window

Except from lacking semicolons between the statements, yes.
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of Zado

ASKER

I never heard about UNION, it works great, thanks a lot.
Glad I could help!

Chris