gpt4 book ai didi

mysql查询错误

转载 作者:太空宇宙 更新时间:2023-11-03 10:30:52 25 4
gpt4 key购买 nike

select id, s
from (
select o_user_id as id, sum(total_price) as s
from Orders o
group by o.o_user_id
) as t1
where s = (select max(t1.s) from t1)

它返回一个错误,表示表 t1 不存在。

我想找出所有订单中消费最多的用户的id

这是订单表 enter image description here

最佳答案

该别名超出了子查询的范围

select id, s
from (
select o_user_id as id, sum(total_price) as s
from Orders o
group by o.o_user_id
) as t1
where s = (select max(t1.s) from t1)

你可以做到

WITH T1 AS 
(
select o_user_id as id, sum(total_price) as s
from Orders o
group by o.o_user_id
)
SELECT id, s
FROM T1
WHERE s = (select max(t1.s) from t1);

关于mysql查询错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59219528/

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