MySQL,CROSS JOIN和未知专栏

时间:2016-05-12 07:04:50

标签: mysql sql

我编写了使用子查询的mysql查询。我无法理解为什么mysql会向我展示“未知专栏”和“TEGETID'在' where子句''在子查询中。对于MSSQL,它没有问题。

SELECT @PERIODTYPE := 'Y';


SELECT
  @PERIODTYPE,
  CASE 
    WHEN @PERIODTYPE = 'W' THEN DATE_FORMAT(R.DATENEW,'%u')
    WHEN @PERIODTYPE = 'M' THEN DATE_FORMAT(R.DATENEW,'%Y-%m')
    WHEN @PERIODTYPE = 'Y' THEN DATE_FORMAT(R.DATENEW,'%Y')
    ELSE DATE_FORMAT(R.DATENEW,'%Y-%m-%d') 
  END `Period`,
  DATE_FORMAT(MIN(R.DATENEW),'%Y-%m-%d') min_date,
  DATE_FORMAT(MAX(R.DATENEW),'%Y-%m-%d') max_date,
  COUNT(*) Transactions,
  SUM(SQ.`Sold Units`)  `Sold Units`,
  FORMAT(MAX(P.TOTAL), 2) `Largest Order`, 
  FORMAT(SUM(P.TOTAL), 2) `Total $ Sold`,
  FORMAT(SUM(TXL.AMOUNT), 2) `Total Tax $ Collected`
FROM RECEIPTS R INNER JOIN TAXLINES TXL ON R.ID = TXL.RECEIPT
                INNER JOIN TAXES TX ON TXL.TAXID = TX.ID 
                INNER JOIN TAXCATEGORIES TXC ON TX.CATEGORY = TXC.ID 
                INNER JOIN PAYMENTS P ON P.RECEIPT = R.ID
                INNER JOIN TICKETS T ON R.ID = T.ID  
                CROSS JOIN (SELECT SUM(TL.UNITS) 'Sold Units' FROM TICKETLINES TL WHERE T.TICKETID = TL.TICKET)  SQ
GROUP BY CASE 
    WHEN @PERIODTYPE = 'W' THEN DATE_FORMAT(R.DATENEW,'%u')
    WHEN @PERIODTYPE = 'M' THEN DATE_FORMAT(R.DATENEW,'%Y-%m')
    WHEN @PERIODTYPE = 'Y' THEN DATE_FORMAT(R.DATENEW,'%Y')
    ELSE DATE_FORMAT(R.DATENEW,'%Y-%m-%d')
  END
ORDER BY R.DATENEW

更新1

我替换了表名(TICKETS)而不是别名(T)。我收到了错误。请看截图。 enter image description here

SELECT @PERIODTYPE := 'D';


SELECT
  @PERIODTYPE,
  CASE 
    WHEN @PERIODTYPE = 'W' THEN DATE_FORMAT(R.DATENEW,'%u')
    WHEN @PERIODTYPE = 'M' THEN DATE_FORMAT(R.DATENEW,'%Y-%m')
    WHEN @PERIODTYPE = 'Y' THEN DATE_FORMAT(R.DATENEW,'%Y')
    ELSE DATE_FORMAT(R.DATENEW,'%Y-%m-%d') 
  END `Period`,
  DATE_FORMAT(MIN(R.DATENEW),'%Y-%m-%d') min_date,
  DATE_FORMAT(MAX(R.DATENEW),'%Y-%m-%d') max_date,
  COUNT(*) Transactions,
  FORMAT(SUM(IFNULL(SQ.`Sold Units`,0)),2)  `Sold Units`,
  FORMAT(MAX(P.TOTAL), 2) `Largest Order`, 
  FORMAT(SUM(P.TOTAL), 2) `Total $ Sold`,
  FORMAT(SUM(TXL.AMOUNT), 2) `Total Tax $ Collected`
FROM RECEIPTS R INNER JOIN TAXLINES TXL ON R.ID = TXL.RECEIPT
                INNER JOIN TAXES TX ON TXL.TAXID = TX.ID 
                INNER JOIN TAXCATEGORIES TXC ON TX.CATEGORY = TXC.ID 
                INNER JOIN PAYMENTS P ON P.RECEIPT = R.ID
                INNER JOIN TICKETS ON R.ID = TICKETS.ID 
--                LEFT JOIN (SELECT TL.TICKET, SUM(TL.UNITS) 'Sold Units' FROM TICKETLINES TL  GROUP BY TL.TICKET)  SQ ON T.ID = SQ.TICKET
                CROSS JOIN (SELECT SUM(TL.UNITS) 'Sold Units' FROM TICKETLINES TL WHERE  TICKETS.ID = SQ.TICKET)  SQ 

GROUP BY CASE 
    WHEN @PERIODTYPE = 'W' THEN DATE_FORMAT(R.DATENEW,'%u')
    WHEN @PERIODTYPE = 'M' THEN DATE_FORMAT(R.DATENEW,'%Y-%m')
    WHEN @PERIODTYPE = 'Y' THEN DATE_FORMAT(R.DATENEW,'%Y')
    ELSE DATE_FORMAT(R.DATENEW,'%Y-%m-%d')
  END
ORDER BY R.DATENEW

1 个答案:

答案 0 :(得分:0)

查询无法在mysql中工作因为mysql不能使用声明为ad的别名,而是使用上层级别的子查询...

在交叉连接的子查询中

CROSS JOIN (SELECT SUM(TL.UNITS) 'Sold Units' 
         FROM TICKETLINES TL WHERE T.TICKETID = TL.TICKET)  SQ

您使用T.TICKETID

别名T

  INNER JOIN TICKETS T ON R.ID = T.ID  

在一个不是"可访问的地方宣布"由子查询的范围..

然后,如果可能的话,我建议你构建一个不引用T别名的子查询。尝试(如果可能的话)构建一个没有别名的等效子查询