简体   繁体   English

如何编写包含左联接的创建表语句?

[英]How to write the create table statement which contains a left join?

I can get a left join with the following mysql statement: Table fields defination:我可以使用以下 mysql 语句获得左连接:表字段定义:

 show columns from back;
+-------------------------+------------+------+-----+---------+-------+
| Field                   | Type       | Null | Key | Default | Extra |
+-------------------------+------------+------+-----+---------+-------+
| code                    | text       | YES  |     | NULL    |       |
| report_date             | varchar(4) | YES  |     | NULL    |       |
| total_operating_revenue | double     | YES  |     | NULL    |       |
+-------------------------+------------+------+-----+---------+-------+
3 rows in set (0.01 sec)

I have uploaded the table into dropbox我已将表格上传到保管箱

back table download for test mysql statement用于测试mysql语句的回表下载

My query.我的查询。

select * from ((
    select *  from  back   
    ) a 
left join(    
    select * from  back  
    ) b on  a.report_date  = b.report_date + 1 
            and  a.code = b.code);

Now i want to rewrite it as a create table statement:现在我想将它重写为一个create table语句:

create table result as(( 
    select *  from  back   
    ) a 
left join(    
    select * from  back  
    ) b on  a.report_date  = b.report_date + 1 
            and  a.code = b.code);

I encounter issue:我遇到问题:

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '( 
    select *  from  back   
    ) a 
left join(    
    select * from  back  ' at line 1

How to write the create table statement which contains a left join?如何编写包含左联接的创建表语句?
@Barmar,both method encounter same error:ERROR 1060 (42S21): Duplicate column name 'code' @Barmar,两种方法都遇到相同的错误:ERROR 1060 (42S21): Duplicate column name 'code'

MariaDB [finance]> create table result as( 
    -> select * from ((
    ->     select *  from  back   
    ->     ) a 
    -> left join(    
    ->     select * from  back  
    ->     ) b on  a.report_date  = b.report_date + 1 
    ->             and  a.code = b.code));

ERROR 1060 (42S21): Duplicate column name 'code'错误 1060 (42S21):列名“代码”重复

The second way:第二种方式:

MariaDB [finance]> CREATE TABLE result AS
    -> SELECT *
    -> FROM back AS a
    -> LEFT JOIN back AS b ON a.report_date = b.report_date + 1 AND a.code = b.code;
ERROR 1060 (42S21): Duplicate column name 'code'

My latest try:我最近的尝试:

create table result as 
select a.code,a.report_date,a.total_operating_revenue,b.code,b.report_date,b.total_operating_revenue from (
    select a.code,a.report_date,a.total_operating_revenue  from  back   
    ) a 
left join(    
    select b.code,b.report_date,b.total_operating_revenue from  back  
    ) b on  a.report_date  = b.report_date + 1 
            and  a.code = b.code;

Unknown column error:未知列错误:

ERROR 1054 (42S22): Unknown column 'a.code' in 'field list'

Just put create table result before the original query.只需将create table result放在原始查询之前。

create table result as
select * from ((
    select *  from  back   
    ) a 
left join(    
    select * from  back  
    ) b on  a.report_date  = b.report_date + 1 
            and  a.code = b.code);

or without all the subqueries:或者没有所有子查询:

CREATE TABLE result AS
SELECT *
FROM back AS a
LEFT JOIN back AS b ON a.report_date = b.report_date + 1 AND a.code = b.code

However, using a self-join with SELECT * will try to create duplicate column names in the new table, which I think will cause another error.但是,使用带有SELECT *的自SELECT *会尝试在新表中创建重复的列名,我认为这会导致另一个错误。 You should select specific columns, and if you need the same column from both a and b you must give them different aliases for the new table columns.您应该选择特定的列,如果您需要ab的相同列,则必须为新表列指定不同的别名。

CREATE TABLE result AS
select * from ((
    select code as a_code ,report_date as a_report_date,total_operating_revenue as a_revenue from  back   
    ) a 
left join(    
    select code as b_code ,report_date as b_report_date,total_operating_revenue as b_revenue  from  back 
    ) b on  a_report_date  = b_report_date + 1 
            and  a_code = b_code);

edit编辑

You could use this query to create a table with a left join and your condition您可以使用此查询创建一个带有左连接和您的条件的表

create table result as
select a.* from back a
left join back b on (a.report_date = b.report_date + 1) and a.code = b.code;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM