别名在where子句中导致错误 - MySQL

时间:2016-06-01 11:46:08

标签: mysql sql where

我的SQL代码:

SELECT (
           SELECT COUNT(1)
           FROM   FAVORI_DUALAR
           WHERE  DuaID       = D.DuaID
                  AND PID     = 'L3FNCPEVME36'
       )       AS Eklimi,
       (
           SELECT COUNT(1)
           FROM   EDILEN_DUALAR
           WHERE  DuaID       = D.DuaID
                  AND PID     = 'L3FNCPEVME36'
       )       AS Okudum,
       D.Tarih,
       D.DuaID,
       D.DuaBaslik,
       D.DuaTuru,
       D.DuaSayisiSiniri,
       D.DuaIcerik,
       D.DuaMeal,
       D.DuaArapca,
       D.PID,
       (
           SELECT COUNT(1)
           FROM   EDILEN_DUALAR
           WHERE  DuaID = D.DuaID
       )       AS Okundu
FROM   DUALAR     D
WHERE  D.Aktif = '1'
       AND (D.DuaTuru = 2 OR D.DuaTuru = 1 AND Okudum = 0)
ORDER BY
       D.Tarih DESC 
       LIMIT 9
  

错误:#1054 - 未知列' Okudum'在' where子句'

我写了

SELECT COUNT(1)
FROM   EDILEN_DUALAR
WHERE  DuaID       = D.DuaID
       AND PID     = 'L3FNCPEVME36') AS Okudum

但我仍然在查询中收到错误。

3 个答案:

答案 0 :(得分:2)

您的Okudum字段是查询中的计算字段。这不能直接引用(仅在ORDER BY中)。

所以你可以在where子句中使用:

WHERE ... AND (SELECT COUNT(1) FROM   EDILEN_DUALAR WHERE DuaID = D.DuaID AND PID = 'L3FNCPEVME36') = 0

或者你可以做一个子查询: 使用EDILEN_DUALAR作为子查询进行连接。

SELECT (
           SELECT COUNT(1)
           FROM   FAVORI_DUALAR
           WHERE  DuaID       = D.DuaID
                  AND PID     = 'L3FNCPEVME36'
       )       AS Eklimi,
       ED.Okudum,
       D.Tarih,
       D.DuaID,
       D.DuaBaslik,
       D.DuaTuru,
       D.DuaSayisiSiniri,
       D.DuaIcerik,
       D.DuaMeal,
       D.DuaArapca,
       D.PID,
       (
           SELECT COUNT(1)
           FROM   EDILEN_DUALAR
           WHERE  DuaID = D.DuaID
       )       AS Okundu
, D.Aktif
FROM   DUALAR D LEFT JOIN
    (SELECT DuaID, COUNT(1) AS Okudum
           FROM   EDILEN_DUALAR
           WHERE  PID     = 'L3FNCPEVME36'
                  GROUP BY DuaID       ) ED

ON ED.DuaID = D.DuaID
WHERE   D.Aktif = '1'
       AND (D.DuaTuru = 2 OR D.DuaTuru = 1 AND (ED.Okudum IS NULL OR ED.Okudum = 0))
ORDER BY
       D.Tarih DESC 

答案 1 :(得分:0)

您需要使用having子句代替where。否则你需要提供完整的参考名称和表格 我编辑我的答案,请参阅

SELECT ( SELECT COUNT(1) FROM FAVORI_DUALAR WHERE DuaID = D.DuaID AND PID = 'L3FNCPEVME36' ) AS Eklimi, ( SELECT COUNT(1) FROM EDILEN_DUALAR WHERE DuaID = D.DuaID AND PID = 'L3FNCPEVME36' ) AS Okudum, D.Tarih, D.DuaID, D.DuaBaslik, D.DuaTuru, D.DuaSayisiSiniri, D.DuaIcerik, D.DuaMeal, D.DuaArapca, D.PID, ( SELECT COUNT(1) FROM EDILEN_DUALAR WHERE DuaID = D.DuaID ) AS Okundu FROM DUALAR D having D.Aktif = '1' AND (D.DuaTuru = 2 OR D.DuaTuru = 1 AND Okudum = 0) ORDER BY D.Tarih DESC

答案 2 :(得分:0)

你可以试试这个。假设您在DUALAR.AktifDUALAR.DuaTuru中有2个索引,这应该比我的第一个答案运行得快。

select 
    (
       SELECT COUNT(1)
       FROM   FAVORI_DUALAR
       WHERE  DuaID       = D.DuaID
              AND PID     = 'L3FNCPEVME36'
   )       AS Eklimi,
   D.Okudum,
   D.Tarih,
   D.DuaID,
   D.DuaBaslik,
   D.DuaTuru,
   D.DuaSayisiSiniri,
   D.DuaIcerik,
   D.DuaMeal,
   D.DuaArapca,
   D.PID,
   (
       SELECT COUNT(1)
       FROM   EDILEN_DUALAR
       WHERE  DuaID = D.DuaID
   )       AS Okundu
from
    (select 
        (
            SELECT COUNT(1)
            FROM   EDILEN_DUALAR
            WHERE  DuaID       = d1.DuaID
                  AND PID     = 'L3FNCPEVME36'
        )       AS Okudum
        ,d1.*
    FROM   
        DUALAR     d1
    WHERE  
        d1.Aktif = '1' 
        and d1.DuaTuru = 2
    union all
    select 
        (
            SELECT COUNT(1)
            FROM   EDILEN_DUALAR
            WHERE  DuaID       = d2.DuaID
                  AND PID     = 'L3FNCPEVME36'
        )       AS Okudum
        ,d2.*
    FROM   
        DUALAR     d2
    WHERE  
        d2.Aktif = '1' 
        and d2.DuaTuru = 1
    ) D
where
    (D.DuaTuru = 2 OR D.DuaTuru = 1 AND D.Okudum = 0)
ORDER BY
    D.Tarih DESC 
LIMIT 9;