如何在mysql的where子句中使用别名

时间:2012-01-01 12:34:12

标签: mysql

还有其他方法可以执行此查询:

SELECT CONCAT_WS(" ", strLname, strFname, strMname) AS lessor_name 
FROM tbl_lessor 
WHERE lessor = '$lessor_name'

我尝试了但是在mysql中显示了未知列的错误

4 个答案:

答案 0 :(得分:10)

可能是由于lessor而不是where子句中的lessor_name。所以请尝试以下一次:

SELECT CONCAT_WS(" ", strLname, strFname, strMname) AS lessor_name 
FROM tbl_lessor 
having lessor_name= '$lessor_name'

答案 1 :(得分:4)

在MySQL中,您不能使用 WHERE 附近的别名。

答案 2 :(得分:2)

也许这会奏效:

SELECT CONCAT_WS(" ", strLname, strFname, strMname) AS lessor_name  
FROM tbl_lessor  
WHERE CONCAT_WS(" ", strLname, strFname, strMname) = '$lessor_name' 

答案 3 :(得分:0)

这样可行。

SELECT * FROM (SELECT CONCAT_WS(" ", strLname, strFname, strMname) AS lessor_name 
FROM tbl_lessor) AS t1 
WHERE lessor_name = '$lessor_name'