【问题标题】:MY subquery does not work with error message我的子查询不适用于错误消息
【发布时间】:2022-01-02 06:09:13
【问题描述】:

当我运行下面显示的查询时,它会给出错误消息。请帮我改正

select Debit_ID,Debit_Date,Debit_Note,Deb_Code,Debit_Amount,Settlement,Debit_Amount-Settlement as Balance
 from

(SELECT id Debit_ID,date Debit_Date ,reference_no Debit_Note ,customer_id Deb_Code,total Debit_Amount,
(select nvl(sum(amount),0) from sma_payments where type='received' and sma_sales.id=sma_payments.sale_id)Settlement
 from sma_sales where customer_id<>1 and sma_sales.sale_status='completed')

[Err] 1064 - 您的 SQL 语法有错误;检查与您的 MariaDB 服务器版本相对应的手册,以获取正确的语法,以便在第 6 行的 '' 附近使用

【问题讨论】:

    标签: mysql mariadb


    【解决方案1】:

    FROM 子句中的子查询需要一个表说明符,即末尾的 AS T

    SELECT
        Debit_ID,
        Debit_Date,
        Debit_Note,
        Deb_Code,
        Debit_Amount,
        Settlement,
        Debit_Amount-Settlement AS Balance 
    FROM
        (
        SELECT
            id Debit_ID,
            date Debit_Date,
            reference_no Debit_Note,
            customer_id Deb_Code,
            total Debit_Amount,
            ( SELECT nvl ( sum( amount ), 0 ) FROM sma_payments WHERE type = 'received' AND sma_sales.id = sma_payments.sale_id ) Settlement 
        FROM
            sma_sales 
        WHERE
        customer_id <> 1 
        AND sma_sales.sale_status = 'completed') AS T;
    

    有关更多信息,您可以查阅 MariaDB 文档: https://mariadb.com/kb/en/subqueries-in-a-from-clause/

    专业提示:正确格式化 SQL 查询时,调试起来会容易得多。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-10
      • 2021-05-03
      • 2021-11-20
      • 2023-02-02
      • 2017-10-25
      • 1970-01-01
      相关资源
      最近更新 更多