MySQL:加入表本身。未知列

时间:2013-11-02 05:32:08

标签: mysql inner-join database-table

疯了!!!!

On 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等等。

我确定。这是一个内心联合的诅咒!

Here the fiddle错误


我不能在同一个子查询中使用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 FIDDLE

由于

的奥斯卡

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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