【问题标题】:Run one query inside another phpmyadmin在另一个 phpmyadmin 中运行一个查询
【发布时间】:2017-03-27 19:43:53
【问题描述】:

我是 SQL 和 phpmyadmin 的新手,所以如果不清楚,我深表歉意。我正在尝试将查询用作来自不同查询的数据源,但我收到来自 SELECT 的解析错误。这就是我想要做的。

select CUS_CODE,
    COUNT(LINE.INV_NUMBER) as "Number of Invoices",
    AVG("Invoice Total") as "Average Invoice Amount",
    MAX("Invoice Total") as "Max Invoice Amount",
    MIN("Invoice Total") as "Min Invoice Amount",
    Sum("Invoice Total") as "Total Customer Purchases"
from (
    select CUS_CODE,
        LINE.INV_NUMBER as INV_NUMBER,
        Sum(LINE.LINE_UNITS * LINE.LINE_PRICE) as "Invoice Total"
    from INVOICE,
        LINE
    where INVOICE.INV_NUMBER = LINE.INV_NUMBER
    group by CUS_CODE,
        LINE.INV_NUMBER
    )
group by CUS_CODE;

【问题讨论】:

  • @GurV 仍然遇到同样的错误 - SELECT 处解析错误

标签: mysql sql phpmyadmin


【解决方案1】:

试试这个:

select cus_code,
    count(inv_number) as number_of_invoices,
    avg(invoice_total) as average_invoice_amount,
    max(invoice_total) as max_invoice_amount,
    min(invoice_total) as min_invoice_amount,
    sum(invoice_total) as total_customer_purchases
from (
    select cus_code,
        l.inv_number,
        sum(l.line_units * l.line_price) as invoice_total
    from invoice i
    join line l on i.inv_number = l.inv_number
    group by cus_code,
        l.inv_number
    ) t
group by cus_code;

几个cmets:

  • 使用现代显式连接语法而不是旧的基于逗号的语法。
  • 为子查询使用别名
  • LINE.INV_NUMBER 在子查询之外无效。直接使用INV_NUMBER
  • 不要使用非标准别名。不要使用空格和反引号,而是使用下划线_

【讨论】:

  • @RyanB - 刚刚更新。请立即尝试?
  • @RyanB - 你能模拟rextester.com/l/mysql_online_compiler 上的错误并在此处分享链接吗?
  • @RyanB - 请在演示中也包含表 DDL。目前那里有一些其他的表,但没有一个是 cus_code 存在的。
猜你喜欢
  • 1970-01-01
  • 2017-08-20
  • 2017-08-21
  • 2023-03-18
  • 2016-08-09
  • 2011-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多