MYSQL中如何把SELECT A AS B中的B作为WHERE筛选条件
PHPz
PHPz 2017-04-17 15:36:18
0
4
637

比如SELECT a AS b WHRER b=1;

我这样使用会报错,说b不存在。

PHPz
PHPz

学习是最好的投资!

répondre à tous(4)
迷茫

因为mysql底层跑SQL语句时:where 后的筛选条件在先, as B的别名在后。
所以机器看到where 后的别名是不认的,所以会报说B不存在。

如果非要用B做筛选条件的话:
解决方案:外边再嵌套一层。
select * from
(
select A as B from table
) t
where t.B = XXX -- 任意的筛选条件

如果不嵌套,只能用A做筛选条件了

大家讲道理

当然不存在,这个b只针对与a查询结果的一个别名
select a AS b FROM table where a = 1

巴扎黑

select t.b from
(

select a as B from table

) t

where t.b =xxxx

此时的B是可以直接在where中使用的

巴扎黑

b是对查询结果a取的别名where里面肯定是不存在啊

Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!