我正在做一个使用TDengine的项目,最近的工作需要做一些子查询。我在做TDengine的子查询实验,遇到了以下问题
taos> select avg(col2) from stb where col1 > (select avg(col1) from stb);
DB error: syntax error near "select avg(col1) from stb);" (0.000149s)
taos> select avg(col2), (select last(ts) from stb_54) as ts_spec from stb;
DB error: syntax error near "select last(ts) from stb_54) as ts_spec from (0.000092s)
taos>select max(col3) from (select spread(col3) as col3 from stbgroup by tbname);
Segmentation fault (core dumped)
TDengine支持子查询吗?是否有我需要遵循的语法?
最佳答案
根据其官方文档,TAOS SQL与MYSQL略有不同,因为它是面向IOT海量数据处理场景的。它提供了一种标准的SQL风格的语言,使用户可以在不支持MySQL所有通用功能的情况下快速上手。
( https://www.taosdata.com/en/documentation/taos-sql )
- 'FROM 关键字后面可以跟几个表 (STables) 的列表或子查询的结果'。 TAOS SQL只支持FROM关键字后的子查询,不支持作为WHERE子句参数的子查询,你可以尝试修改SQL的语法为:
taos> select avg(col2) from (select * from sub where col1>AVG(col1));
数据库错误:无效操作:不支持普通列连接(0.000118s)
结果显示TAOS还不支持column join(where子句中的平均操作)。所以分为两部分:
(1)计算并记录col1的平均值avg1
taos> 从子中选择 AVG(col1);
(2) 将平均值设为常数
taos> select avg(col2) from (select * from sub where col1>avg1);
同1的情况,TAOS SQL不支持多选语法。
经过大量测试,我发现 MAX() 函数可能存在一些不合理的问题,解决此问题的最简单方法是改用 TOP() 函数:
taos> select top(col3,1) from (select spread(col3) as col3 from stb group by tbname);
关于database - TDengine子查询核心转储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68412474/