mysql查询里面的未知列

时间:2017-02-09 03:49:22

标签: mysql

我正在尝试使用SELECT t1.username, t1.user_type, COALESCE(t2.num_appearences, 0) AS `color` FROM user t1 LEFT JOIN ( SELECT username, process, COUNT(DISTINCT username, process) AS num_appearences FROM user_project GROUP BY username ) t2 ON t1.username = t2.username WHERE `color` IN ('0') GROUP BY username 语句进行查询:

#1054 - Unknown column 'color' in 'where clause'

问题是它给了我错误:color如果我尝试使用任何其他列,它的工作除了ls -haltr /proc/$PID/fd/ | cut -f3 -d":" | cut -f2 -d"[" | cut -f1 -d"]" | grep '^[0-9][0-9]*$' | sort -h 列。我该如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

你不能在where子句中使用盟友,别名可以在GROUP BY,ORDER BY或HAVING子句中使用。

Reference

使用

查询替换
SELECT t1.username, t1.user_type, 
COALESCE(t2.num_appearences, 0) AS `color`
FROM user t1
LEFT JOIN
(
    SELECT username, process, COUNT(DISTINCT username, process) AS num_appearences
    FROM user_project
    GROUP BY username
) t2
ON t1.username = t2.username GROUP BY username HAVING `color` IN ('0')    

答案 1 :(得分:1)

您不能在WHERE子句中使用别名。

引自using coalesce or if results in where clause mysql

  

你必须重复自己。

     

WHERE COALESCE(group.display,item.display) = '1'