[mysql 别名as]select … as不能当做字段一样使用

$SELECT name AS nm1 FROM student WHERE nm1 LIKE "%Wang%";

执行会报以下错误:

[Err] 1054 - Unknown column 'nm1' in 'where clause'

原因:别名只是对字段的一种引用,不能当做字段一样使用。

变通策略:

$SELECT nm1 FROM (SELECT name AS nm1 FROM student)AS temptab WHERE nm1 LIKE "%Wang%";

发表评论

Your email address will not be published.