0

发疯!!!!

在 Access 上工作,在 MySQL 上不想要。我在这里的帖子上采取了窍门。

这里是查询:

SELECT A.B_ID,
       B.CNT_B,
       B.MAX_PRICE,
       B.SUM_AB,
       B.SUM_C_AB,
       B.UNIQUE_B,
       B.Costi,
       B.COUNTDOWN,
       A.AUCT_ID
FROM MONITOR AS A
INNER JOIN
  (SELECT B_ID,
          COUNT(B_ID) AS CNT_B,
          MAX(PREZZO_ATTUALE) AS MAX_PRICE,
          SUM(TOT_PUNT_AB) AS SUM_AB,
          SUM(TOT_CAMBI_AB) AS SUM_C_AB,
          (CNT_B-SUM_AB) AS UNIQUE_B,
          (0.035*3*CNT_B+MAX_PRICE) AS COSTI,
          45-SUM(CONT_PROVV_AB) AS COUNTDOWN
   FROM MONITOR
   WHERE AUCT_ID = '123456789'
   GROUP BY B_ID) AS B
  ON A.B_ID= B.B_ID
WHERE A.AUCT_ID = '123456789'
ORDER BY B.MAX_PRICE DESC,
         B.CNT_B DESC

节目

错误:[MySQL][ODBC 3.51 驱动程序][mysqld-5.5.33-31.1-log]“字段列表”中的未知列“CNT_B”

如果我离开 CNT_B,它会告诉我 MAX_PRICE 等等。

我确定。这是一个 INNER JOIN 诅咒!

这里是错误的小提琴


解决方案

我不能在同一个子查询中使用 ALIAS 值。

所以新的 VALID 查询是:

   SELECT A.B_ID,
           B.CNT_B,
           B.MAX_PRICE,
           B.SUM_AB,
           B.SUM_C_AB,
           B.UNIQUE_B,
           B.Costi,
           B.COUNTDOWN,
           A.AUCT_ID
    FROM MONITOR AS A
    INNER JOIN
      (SELECT B_ID,
              COUNT(B_ID) AS CNT_B,
              MAX(PREZZO_ATTUALE) AS MAX_PRICE,
              SUM(TOT_PUNT_AB) AS SUM_AB,
              SUM(TOT_CAMBI_AB) AS SUM_C_AB,
              (COUNT(B_ID)-SUM(TOT_PUNT_AB)) AS UNIQUE_B,
              (0.035*3*COUNT(B_ID)+MAX(PREZZO_ATTUALE)) AS COSTI,
              45-SUM(CONT_PROVV_AB) AS COUNTDOWN
       FROM MONITOR
       WHERE AUCT_ID = '123456789'
       GROUP BY B_ID) AS B
      ON A.B_ID= B.B_ID
    WHERE A.AUCT_ID = '123456789'
    ORDER BY B.MAX_PRICE DESC,
             B.CNT_B DESC

SQL 小提琴

谢谢

奥斯卡

4

2 回答 2

1

从子查询向上移动到主查询:

 (CNT_B-SUM_AB) AS UNIQUE_B,
 (0.035*3*CNT_B+MAX_PRICE) AS COSTI,

因为您试图从函数本身调用别名。Computed Aggregate

这样你就会有这个:

SELECT A.B_ID,
   B.CNT_B,
   B.MAX_PRICE,
   B.SUM_AB,
   B.SUM_C_AB,
   B.CNT_B-B.SUM_AB AS UNIQUE_B,
   (0.035*3*B.CNT_B+B.MAX_PRICE) AS COSTI,
   B.COUNTDOWN,
   A.AUCT_ID
FROM MONITOR A
INNER JOIN
 (SELECT B_ID,
      COUNT(B_ID) AS CNT_B,
      MAX(PREZZO_ATTUALE) AS MAX_PRICE,
      SUM(TOT_PUNT_AB) AS SUM_AB,
      SUM(TOT_CAMBI_AB) AS SUM_C_AB,
      45-SUM(CONT_PROVV_AB) AS COUNTDOWN
FROM MONITOR
WHERE AUCT_ID = '3453015'
GROUP BY B_ID) B
ON A.B_ID= B.B_ID
WHERE A.AUCT_ID = '3453015'
ORDER BY B.MAX_PRICE DESC,
     B.CNT_B DESC

请参阅SQLFiddle

于 2013-11-04T01:40:26.317 回答
0

你可以试试这个,它不引用C表:

SELECT A.B_ID,
       B.CNT_B,
       B.MAX_PRICE,
       B.SUM_AB,
       B.SUM_C_AB,
       B.UNIQUE_B,
       B.Costi,
       B.COUNTDOWN,
       A.AUCT_ID
FROM MONITOR AS A
INNER JOIN
  (SELECT B_ID,
          COUNT(B_ID) AS CNT_B,
          MAX(PREZZO_ATTUALE) AS MAX_PRICE,
          SUM(TOT_PUNT_AB) AS SUM_AB,
          SUM(TOT_CAMBI_AB) AS SUM_C_AB,
          (CNT_B-SUM_AB) AS UNIQUE_B,
          (0.035*3*CNT_B+MAX_PRICE) AS COSTI,
          45-SUM(CONT_PROVV_AB) AS COUNTDOWN
   FROM MONITOR
   WHERE AUCT_ID = '123456789'
   GROUP BY B_ID) AS B
  ON A.B_ID= B.B_ID
WHERE A.AUCT_ID = '123456789'
ORDER BY B.MAX_PRICE DESC,
         B.CNT_B DESC
于 2013-11-02T05:53:33.537 回答