Somk
Somk

Reputation: 12057

MySQL Merge Two tables

How can I merge two tables into one table with a query?

I currently have tablea such that it has two columns Zone and Number Then I have another table called Zones and this has Zone, Latitude, Longitude.

I want to end up with tablec containing Zone, Number, Latitude, Longitude.

But I don't know if I can do this with just a query. Normally I would run a PHP script query with a JOIN statement.

Upvotes: 0

Views: 86

Answers (1)

scragar
scragar

Reputation: 6824

If you want a new "table" and don't mind not being able to insert data you could create a view, something like

CREATE VIEW tablec AS
  SELECT
      A.Zone,
      A.Number,
      B.Latitude,
      B.Longitude
  FROM tablea AS A
  INNER JOIN tableb AS B
  ON A.Zone = B.Zone`

Or you could create the table, but this would lose your references(so if you update tablea or tableb they could get out of sync):

CREATE TABLE tablec
  SELECT
      A.Zone,
      A.Number,
      B.Latitude,
      B.Longitude
  FROM tablea AS A
  INNER JOIN tableb AS B
  ON A.Zone = B.Zone`

Upvotes: 1

Related Questions