mysql - View 不允许在 FROM 子句中使用子查询,如何在没有子查询的情况下重写我的 SQL?

标签 mysql sql database sql-view

我写了一条sql来从Mysql中的两个表中选择记录:

select * from(
    select
    IFNULL(a.father_id,0) as afi,
    IFNULL(b.father_id, 0) as bfi,
    IFNULL(a.id,0) AS aid,
    IFNULL(b.id,0) AS bid,
    a.competitor AS competitor,
    IFNULL(a.merchant_order_no,b.merchant_order_no) AS order_no,
    IFNULL(a.income,0) AS yingshou,IFNULL(b.income ,0) AS shishou,IFNULL(b.expenditure,0) AS shouxufei,a.apply_time AS apply_time,a.pay_time AS pay_time,b.trading_date AS trading_date,
    IFNULL(a.trading_channel,b.trading_channel) as channel,
    a.race_id,
    a.group_id
        from (azim_raceapplicant_export_xls a left join azim_finance_export_xls b on(a.merchant_order_no = b.merchant_order_no  ))
        union
    select
    IFNULL(a.father_id,0) as afi,
    IFNULL(b.father_id, 0) as bfi,

    IFNULL(a.id,0) AS aid,
    IFNULL(b.id,0) AS bid,
    a.competitor AS competitor,
    IFNULL(a.merchant_order_no,b.merchant_order_no) AS order_no,
    IFNULL(a.income,0) AS yingshou,IFNULL(b.income ,0) AS shishou,IFNULL(b.expenditure,0) AS shouxufei,a.apply_time AS apply_time,a.pay_time AS pay_time,b.trading_date AS trading_date,
    IFNULL(a.trading_channel,b.trading_channel) as channel,
    a.race_id,
    a.group_id
        from (azim_raceapplicant_export_xls a right join azim_finance_export_xls b on(a.merchant_order_no = b.merchant_order_no ))
)
as t
where afi=0 and bfi=0;

当我想为此结果创建 View 时,我收到此错误,并且当我搜索时我知道 View 中的子查询是有限的。

现在我能做的就是使用两个 View 来获得这个结果。

所以我想知道如何在没有子查询的情况下重写这个sql??

最佳答案

您的外部查询是多余的。尝试:

    select
        IFNULL(a.father_id,0) as afi,
        IFNULL(b.father_id, 0) as bfi,
        IFNULL(a.id,0) AS aid,
        IFNULL(b.id,0) AS bid,
        a.competitor AS competitor,
        IFNULL(a.merchant_order_no,b.merchant_order_no) AS order_no,
        IFNULL(a.income,0) AS yingshou,IFNULL(b.income ,0) AS shishou,IFNULL(b.expenditure,0) AS shouxufei,a.apply_time AS apply_time,a.pay_time AS pay_time,b.trading_date AS trading_date,
        IFNULL(a.trading_channel,b.trading_channel) as channel,
        a.race_id,
        a.group_id
            from (azim_raceapplicant_export_xls a left join azim_finance_export_xls b on(a.merchant_order_no = b.merchant_order_no  ))
            union
        select
        IFNULL(a.father_id,0) as afi,
        IFNULL(b.father_id, 0) as bfi,

        IFNULL(a.id,0) AS aid,
        IFNULL(b.id,0) AS bid,
        a.competitor AS competitor,
        IFNULL(a.merchant_order_no,b.merchant_order_no) AS order_no,
        IFNULL(a.income,0) AS yingshou,IFNULL(b.income ,0) AS shishou,IFNULL(b.expenditure,0) AS shouxufei,a.apply_time AS apply_time,a.pay_time AS pay_time,b.trading_date AS trading_date,
        IFNULL(a.trading_channel,b.trading_channel) as channel,
        a.race_id,
        a.group_id
            from azim_raceapplicant_export_xls a right join azim_finance_export_xls b on(a.merchant_order_no = b.merchant_order_no)
    where 
(a.father_id is null or a.father_id=0) and
(b.father_id is null or b.father_id=0);

或者,您可以从内部查询创建 View :

create view V1 as (
 select
    IFNULL(a.father_id,0) as afi,
    IFNULL(b.father_id, 0) as bfi,
    IFNULL(a.id,0) AS aid,
    IFNULL(b.id,0) AS bid,
    a.competitor AS competitor,
    IFNULL(a.merchant_order_no,b.merchant_order_no) AS order_no,
    IFNULL(a.income,0) AS yingshou,IFNULL(b.income ,0) AS shishou,IFNULL(b.expenditure,0) AS shouxufei,a.apply_time AS apply_time,a.pay_time AS pay_time,b.trading_date AS trading_date,
    IFNULL(a.trading_channel,b.trading_channel) as channel,
    a.race_id,
    a.group_id
        from (azim_raceapplicant_export_xls a left join azim_finance_export_xls b on(a.merchant_order_no = b.merchant_order_no  ))
        union
    select
    IFNULL(a.father_id,0) as afi,
    IFNULL(b.father_id, 0) as bfi,

    IFNULL(a.id,0) AS aid,
    IFNULL(b.id,0) AS bid,
    a.competitor AS competitor,
    IFNULL(a.merchant_order_no,b.merchant_order_no) AS order_no,
    IFNULL(a.income,0) AS yingshou,IFNULL(b.income ,0) AS shishou,IFNULL(b.expenditure,0) AS shouxufei,a.apply_time AS apply_time,a.pay_time AS pay_time,b.trading_date AS trading_date,
    IFNULL(a.trading_channel,b.trading_channel) as channel,
    a.race_id,
    a.group_id
        from (azim_raceapplicant_export_xls a right join azim_finance_export_xls b on(a.merchant_order_no = b.merchant_order_no )) 
)

然后

另一个,从中选择:

create view V2 as (select * from V1 where afi=0 and bfi=0)

关于mysql - View 不允许在 FROM 子句中使用子查询,如何在没有子查询的情况下重写我的 SQL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17607187/

相关文章:

php 数组,foreach 键跳过一个值

mysql选择两列之和小于一列的记录

sql - sp_send_dbmail 附加在数据库中存储为 varbinary 的文件

mysql - 从 MySQL 迁移到 SQL Server,约束问题

mysql - 错误: Error #1215: Cannot add foreign key constraint

PHP 看不到 MySQL

mysql - 在子选择中求和返回不正确的值

sql - 如何从 sql 中的临时 #date 表中选择之前的日期?

java - JBoss 中的数据源绑定(bind)

php - mysql - 排序后选择