MySQL - How to create a new table that is a join on primary key of two existing tables

前端 未结 4 1736
旧巷少年郎
旧巷少年郎 2021-02-05 17:39

I have two existing tables, with different fields, except for Primary ID (a varchar, not an int). I want to create a third table which is essentially a

4条回答
  •  长发绾君心
    2021-02-05 18:07

    CREATE TABLE result AS 
      (SELECT first.*, 
              second.f1, 
              second.f2, 
              second.f3 
       FROM   first 
              INNER JOIN second 
                      ON first.id = second.id);
    

    To get a view, do the same except replace "TABLE" with "VIEW". If you go with the table rather than the view, make sure to add a primary key as that will not be added by default.

提交回复
热议问题