[英]Left Join - Unknown Column Error 1054
I keep getting #1054 - Unknown column 'colour' in 'where clause'. 我一直在'where子句'中获得#1054 - 未知列'color'。
A simplified version of my query is as follows. 我的查询的简化版本如下。
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
If I remove the WHERE line the query executed fine and colour is recognised and all is well. 如果我删除WHERE行,查询执行正常,颜色被识别,一切都很好。
Any help would be appreciated. 任何帮助,将不胜感激。
For your updated question i think you can do 对于您更新的问题,我认为您可以做到
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
It's because you can't use an alias in your WHERE
and ORDER BY
clause 这是因为您不能在
WHERE
和ORDER BY
子句中使用别名
So you would have to do something like 所以你必须做类似的事情
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
Or this for your simplified query 或者这是您的简化查询
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
Why sorting by color if it's always red? 如果总是红色,为什么要按颜色分类?
try this WHERE c.status !='1'
试试这个
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
Usage of alias for a column in where clause is not a good approach. 在where子句中使用别名的别名不是一个好方法。 Please refer the following link.
请参考以下链接。
http://dev.mysql.com/doc/refman/5.0/en/problems-with-alias.html http://dev.mysql.com/doc/refman/5.0/en/problems-with-alias.html
Standard SQL disallows references to column aliases in a WHERE clause. 标准SQL不允许在WHERE子句中引用列别名。 This restriction is imposed because when the WHERE clause is evaluated, the column value may not yet have been determined.
强制执行此限制是因为在评估WHERE子句时,可能尚未确定列值。 For example, the following query is illegal:
例如,以下查询是非法的:
SELECT id, COUNT(*) AS cnt FROM tbl_name WHERE cnt > 0 GROUP BY id;
Use HAVING for alias names or change the condition to use the exact column name in WHERE clause. 将HAVING用于别名或更改条件以在WHERE子句中使用确切的列名。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.