在另一个phpmyadmin中运行一个查询

时间:2017-03-27 19:43:54

标签: mysql sql phpmyadmin

我是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;

1 个答案:

答案 0 :(得分:0)

试试这个:

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;

很少有评论:

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