Tony Clifton
Tony Clifton

Reputation: 703

MYSQL / PHP - merge two tables with different columns

I want to access a database merging two tables from a php script in order to output the result to an html table. Both tables are pretty much the same except one column I need only exists in one table. I don't have direct access to the database.

INSERT INTO globalusers
(surname, firstname, email, location)

INSERT INTO localusers
(surname, firstname, email)

Location exists only in globalusers. I need to use this column as a dropdown filter in my table and add the column location with the string "local" to the data coming from the localusers table.

Here's a fiddle

So the expected result would be one table with four columns and the string "local" in the location column for the localusers table.

What is the select that I need?

MySQL version: 5.5.36

Upvotes: 1

Views: 3929

Answers (1)

sgeddes
sgeddes

Reputation: 62831

Sounds like you're looking to combine the results of the two tables. If so, you can use UNION ALL for this:

SELECT surname, firstname, email, location
FROM globalusers
UNION ALL
SELECT surname, firstname, email, 'local'
FROM localusers

Upvotes: 4

Related Questions