gpt4 book ai didi

mysql - MySQL 中 where 子句和别名的问题

转载 作者:行者123 更新时间:2023-11-29 03:45:16 26 4
gpt4 key购买 nike

这个 mysql 查询有什么问题:

select * 
from tpa as t1
where ( select count(*)
from tpa as t2
where t1.id = t2.id )

错误:

Error Code: 1054. Unknown column 't1.id' in 'field list'

最佳答案

我认为,正如 Cfreak 在评论中指出的那样,别名在子查询中是不可见的。

我还认为您忘记了为您的 count(*) 结果指定一些条件,使其等于某个数字(或其他条件):

select * from tpa as t1 
where
(
select count(*) from
(
select * from tpa
)
as t2
where t1.id = t2.c_id
) = 1

用您喜欢的任何数字条件更改“= 1”,否则重写这个更简单的查询将是一种愚蠢的方法:

select distinct * from tpa

:-)

关于mysql - MySQL 中 where 子句和别名的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6694875/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com