首页 > 解决方案 > TDengine 子查询核心转储

问题描述

我正在使用 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 支持子查询吗?有没有我需要遵循的语法?

标签: database

解决方案


根据官方文档,TAOS SQL 与 MYSQL 略有不同,因为它面向物联网中海量数据处理的场景。它为用户提供了一种标准的 SQL 风格的语言,可以在不支持 MySQL 的所有通用功能的情况下快速上手。

( https://www.taosdata.com/en/documentation/taos-sql )

  1. ' FROM关键字后面可以跟几个表(STables)的列表或子查询的结果'。TAOS SQL 只支持 FROM 关键字后的子查询,不支持 WHERE 子句参数的子查询,可以尝试修改 SQL 的语法为:

taos> select avg(col2) from (select * from sub where col1>AVG(col1));

DB错误:无效操作:不支持普通列连接(0.000118s)

结果显示 TAOS 还不支持列连接(where 子句中的平均操作)。所以分成两部分:

(1)计算并记录col1的平均值avg1

taos> 从 sub 中选择 AVG(col1);

(2) 将平均值设为常数

taos> select avg(col2) from (select * from sub where col1> avg1);

  1. 1. TAOS SQL 不支持多选语法。

  2. 经过大量测试,我发现 MAX() 函数可能存在一些不合理的问题,解决这个问题的最简单方法是使用 TOP() 函数代替:

taos> select top(col3,1) from (select spread(col3) as col3 from stb group by tbname);


推荐阅读