gpt4 book ai didi

mysql - 编译语句 : FAILED: SemanticException [Error 10002] 时出错

转载 作者:可可西里 更新时间:2023-11-01 14:48:21 24 4
gpt4 key购买 nike

select d.order_type from migu_td_aaa_order_log_d d where  exists(select 1 
from migu_user r where r.user_id = '156210106' and r.user_num =
d.serv_number) and d.product_id in ('2028594290','2028596512','2028597138' )
order by d.opr_time desc limit 1

为什么上面的sql失败,说明: 失败:SemanticException [错误 10002]:第 4:11 行无效的列引用“opr_time”

但下面的一个有效:

select temp.order_type from (
select d.* from migu_td_aaa_order_log_d d where exists(select 1 from
migu_user r where r.user_id = '156210106' and r.user_num = d.serv_number)
and d.product_id in ('2028594290','2028596512','2028597138' ) order by
d.opr_time desc limit 1) temp;

这个也很好用,而且比第二个更有效率:

select d.* from migu_td_aaa_order_log_d d where  exists(select 1 from 
migu_user r where r.user_id = '156210106' and r.user_num = d.serv_number)
and d.product_id in ('2028594290','2028596512','2028597138' )
order by d.opr_time desc limit 1

我只需要获取 order_type 字段,所以即使第二个有效,但花费的时间要多得多。谁能帮我?非常感谢!

最佳答案

您的第一个查询不起作用,因为在第一个 select 语句中,您只获得一列 (d.order_type),但您试图按另一列 (d .opr_time),您尚未将其包含在您的select 语句

select d.order_type from ...
...
order by d.opr_time desc limit 1

请注意,如果您将 d.opr_time 列添加到您的第一个查询中,它将起作用:

select d.order_type, d.opr_time from ...
...
order by d.opr_time desc limit 1

您的第二个查询有效,因为在子查询中,您选择了 d (d.*) 的所有列,因此当您按 opr_time 排序时,该列存在。 (与第三个查询相同)。

select temp.order_type from (
select d.* ... order by d.opr_time ...

编辑:

根据Hive documentation :

When using group by clause, the select statement can only include columns included in the group by clause. Of course, you can have as many aggregation functions (e.g. count) in the select statement as well.

所以,这个查询:

select d.order_type, d.opr_time from ...
...
order by d.opr_time desc limit 1

也不应该起作用,因为 select 子句有一个额外的列 (d.order_type),它不包含在 group by 子句中。

希望对您有所帮助。

附言This answer关于 SQL 执行顺序的信息可能会有用。

关于mysql - 编译语句 : FAILED: SemanticException [Error 10002] 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43731471/

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