将结果加入新表?

时间:2018-08-27 21:58:19

标签: mysql sql

我可以成功地联接两个表,但是我正在尝试将联接的结果输出到新表中。

以下内容引发语法错误,但是如果省略了“ into”行,则可以使用。 需要明确的是-在加入时没有问题,但在“ into”语句中没有问题。

 SELECT evictions.uniqueid_neighborhoods.id, evictions.uniqueid_neighborhoods.neighbor_1, 
evictions.sanfrancisco_evictions_backup2.Breach,
evictions.sanfrancisco_evictions_backup2.NonPayment,
evictions.sanfrancisco_evictions_backup2.Nuisance,
evictions.sanfrancisco_evictions_backup2.IllegalUse
** This causes code to fail 
into evictions_by_commArea 
from evictions.uniqueid_neighborhoods
inner join evictions.sanfrancisco_evictions_backup2 on evictions.uniqueid_neighborhoods.id = evictions.sanfrancisco_evictions_backup2.id

1 个答案:

答案 0 :(得分:3)

MySQL使用CREATE TABLE AS

CREATE TABLE evictions_by_commArea as
    SELECT un.id, un.neighbor_1, 
           sfe.Breach,
           sfe.NonPayment,
           sfe.Nuisance,
           sfe.IllegalUse
    from evictions.uniqueid_neighborhoods un join
         evictions.sanfrancisco_evictions_backup2 sfe
         on un.id = sfe.id;

我还介绍了功能强大的别名,因此查询更易于编写和阅读。