0

我不断收到 #1054 - 'where 子句' 中的未知列 'color'。

我的查询的简化版本如下。

SELECT *, "red" AS colour
FROM (
    SELECT *,'calls' AS 'tbl', id AS cid FROM calls) as c
WHERE colour='red' 
ORDER BY colour ASC, c.created_date DESC

如果我删除 WHERE 行,查询执行得很好并且颜色被识别并且一切都很好。

任何帮助,将不胜感激。

4

4 回答 4

2

对于您更新的问题,我认为您可以

SELECT * FROM (
SELECT *, "red" AS colour
FROM (
    SELECT *,'calls' AS 'tbl', id AS cid FROM calls) as c
) as v
WHERE colour='red' 
ORDER BY colour ASC, v.created_date DESC
于 2012-10-24T13:26:48.817 回答
0

尝试这个WHERE c.status !='1'

SELECT *, IF(c.status = '1', "green", "red") AS colour
FROM (
    SELECT *,'calls1' AS 'tbl', id AS cid FROM calls1  
    WHERE uid LIKE '%1111%' 
    ORDER BY timestamp DESC
    UNION
    SELECT *,'calls2' AS 'tbl', id AS cid FROM calls2  
    WHERE uid LIKE '%1111%' 
    ORDER BY timestamp DESC
) as c
LEFT JOIN objects AS o ON o.call_id = c.id 
WHERE c.status != '1'
ORDER BY colour ASC, c.timestamp DESC
于 2012-10-24T13:17:00.363 回答
0

这是因为您不能在WHEREandORDER BY子句中使用别名

所以你必须做类似的事情

SELECT *, IF(c.status = '1', "green", "red") AS colour
FROM (
    SELECT *,'calls1' AS 'tbl', id AS cid FROM calls1  
    WHERE uid LIKE '%1111%' 
    ORDER BY timestamp DESC
    UNION
    SELECT *,'calls2' AS 'tbl', id AS cid FROM calls2  
    WHERE uid LIKE '%1111%' 
    ORDER BY timestamp DESC
) as c
LEFT JOIN objects AS o ON o.call_id = c.id 
WHERE c.status = 'the color id you want' // or you can rewrite your if here
ORDER BY c.status ASC, c.timestamp DESC

或者这用于您的简化查询

SELECT *, "red" AS colour
FROM (
    SELECT *,'calls' AS 'tbl', id AS cid FROM calls) as c
WHERE c.status = 'the color id you want' 
ORDER BY c.created_date DESC

如果总是红色,为什么要按颜色排序?

于 2012-10-24T13:12:46.390 回答
0

在 where 子句中为列使用别名不是一个好方法。请参考以下链接。

http://dev.mysql.com/doc/refman/5.0/en/problems-with-alias.html


标准 SQL 不允许在 WHERE 子句中引用列别名。施加此限制是因为在评估 WHERE 子句时,可能尚未确定列值。例如,以下查询是非法的:

SELECT id, COUNT(*) AS cnt FROM tbl_name  WHERE cnt > 0 GROUP BY id;

对别名使用 HAVING 或更改条件以在 WHERE 子句中使用确切的列名。

于 2012-10-24T14:55:25.237 回答