简体   繁体   English

结合了子查询和JOIN的MySQL语句不起作用

[英]MySQL statement that combines a sub-query and a JOIN is not working

SELECT i.id AS id, i.modify_date as modify_date, s.subscription as subscriptionid, p.paid/i.total AS paidratio 
    FROM invoices i,
         (SELECT p.invoice, sum(amount) AS paid FROM payments p GROUP BY p.invoice) p
        LEFT JOIN sub_to_inv s 
            ON i.id=s.invoice
    WHERE p.invoice=i.id 
        AND i.corporation='3' 
        AND i.payer=1

The error I get is "unknown column on i.id" which is total bogus - invoices (i) has an id row for sure. 我得到的错误是“ i.id上的未知列”,这是伪造的-发票(i)肯定有一个ID行。 They all do. 他们都做。

The purpose of the sub=query is to find out how much of the invoice has been paid. sub = query的目的是找出已支付了多少发票。 For an invoice that has a "total" column of 1000.00, for example, could have 2 or 3 split payments. 例如,对于“总计”列为1000.00的发票,可以进行2或3次分期付款。 What I ultimately wnat to do here is list all the unpaid invoices or partially invoices first. 我最终在这里要做的是首先列出所有未付发票或部分发票。 But before I even get to the ORDER BY stage, I need to figure out this error. 但是,在进入ORDER BY阶段之前,我需要弄清楚这个错误。

Use JOIN syntax for all joins. 对所有联接使用JOIN语法。 Don't mix JOIN syntax with the comma-style SQL-89 syntax. 不要将JOIN语法与逗号样式的SQL-89语法混合使用。

SELECT ...
FROM invoices i
  INNER JOIN (SELECT...) p
    ON p.invoice=i.id
  LEFT OUTER JOIN sub_to_inv s 
    ON i.id=s.invoice
WHERE 
    AND i.corporation='3' 
    AND i.payer=1

Explanation: JOIN has higher precedence than comma-joins. 说明: JOIN优先级高于逗号联接。 So p JOIN s is evaluated before the query evaluates the join to i . 因此,在查询评估与i p JOIN s之前,先评估p JOIN s Therefore, in the clause ON i.id=s.invoice , the i table is not yet known and is an invalid reference. 因此,在子句ON i.id=s.invoicei表尚不清楚,并且是无效的引用。

See http://dev.mysql.com/doc/refman/5.1/en/join.html , in the doc following "Join Processing Changes in MySQL 5.0.12". 请参阅http://dev.mysql.com/doc/refman/5.1/en/join.html中的“ MySQL 5.0.12中的联接处理更改”之后的文档。

Can you try this ? 你可以试试这个吗?

SELECT i.id AS id, i.modify_date as modify_date, s.subscription as subscriptionid, p.paid/i.total AS paidratio 
    FROM invoices i
        LEFT JOIN
          (SELECT p.invoice, sum(amount) AS paid FROM payments p GROUP BY p.invoice) p
          ON p.invoice=i.id 
        LEFT JOIN sub_to_inv s 
            ON i.id=s.invoice
    WHERE  i.corporation='3' 
        AND i.payer=1

I think you might be running into issues because of the order of your table joins in your SQL. 我认为您可能会遇到问题,因为您的表在SQL中的连接顺序。 Have you tried using an inner join. 您是否尝试过使用内部联接。 Perhaps try: 也许尝试:

SELECT 
  i.id AS id, 
  i.modify_date as modify_date, 
  s.subscription as subscriptionid, 
  p.paid/i.total AS paidratio 
FROM
  invoices i
INNER JOIN
  (SELECT 
    p.invoice,
    sum(amount) AS paid
  FROM
    payments p
  GROUP BY
    p.invoice) p
ON
  p.invoice=i.id
LEFT JOIN
  sub_to_inv s
ON
  i.id=s.invoice
WHERE
  i.corporation='3' AND i.payer=1

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

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