influxDB-查詢操作
轉自:https://www.cnblogs.com/Bluebells/p/14120368.html
1 #----綜合使用 2 書寫順序 3 select distinct * from '表名' where '限制條件' group by '分組依據' having '過濾條件' order by limit '展示條數' 4 執行順序 5 from -- 查詢 6 where -- 限制條件 7 group by -- 分組 8 having -- 過濾條件 9 order by -- 排序 10 limit -- 展示條數 11 distinct -- 去重 12 select -- 查詢的結果
1.查詢數據表weather 的所有記錄:
1
2
3
4
5
6
7
8
9
10
|
>
select
*
from
weather
name
: weather
time
altitude area humidity temperature
---- -------- ---- -------- -----------
1607604432455278300 1001 南 -5 10
1607656595672442800 1000 東 -4 9
1607656662027484500 1001 南 -5 11
1607656706278952000 999 南 -5 11
1607656751612223600 1002 西 -2 11
1607656799728402900 1003 東 -2 11
|
2.按條件查詢
1
2
3
4
5
6
7
8
9
10
|
#查詢temperature=11的數據
>
select
*
from
weather
where
temperature=11
name
: weather
time
altitude area humidity temperature
---- -------- ---- -------- -----------
1607656662027484500 1001 南 -5 11
1607656706278952000 999 南 -5 11
1607656751612223600 1002 西 -2 11
1607656799728402900 1003 東 -2 11
|
1
2
3
4
5
6
7
8
9
10
11
|
#查詢altitude,temperature兩列的數據
>
select
altitude,temperature
from
weather
name
: weather
time
altitude temperature
---- -------- -----------
1607604432455278300 1001 10
1607656595672442800 1000 9
1607656662027484500 1001 11
1607656706278952000 999 11
1607656751612223600 1002 11
1607656799728402900 1003 11
|
3.排序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
#按最新時間排序
>
select
*
from
weather
order
by
time
desc
name
: weather
time
altitude area humidity temperature
---- -------- ---- -------- -----------
1607656799728402900 1003 東 -2 11
1607656751612223600 1002 西 -2 11
1607656706278952000 999 南 -5 11
1607656662027484500 1001 南 -5 11
1607656595672442800 1000 東 -4 9
1607604432455278300 1001 南 -5 10
#按最早時間排序
>
select
*
from
weather
order
by
time
asc
name
: weather
time
altitude area humidity temperature
---- -------- ---- -------- -----------
1607604432455278300 1001 南 -5 10
1607656595672442800 1000 東 -4 9
1607656662027484500 1001 南 -5 11
1607656706278952000 999 南 -5 11
1607656751612223600 1002 西 -2 11
1607656799728402900 1003 東 -2 11
|
4.去重 (distinct)
> select distinct humidity from weather name: weather time distinct ---- -------- 0 -5 0 -4 0 -2
5.group by
select 查詢字段1,查詢字段2,... from 表名 where 過濾條件 group by分組依據 # 分組后取出的是每個組的第一條數據
> select * from weather group by area name: weather tags: area=東 time altitude humidity temperature ---- -------- -------- ----------- 1607656595672442800 1000 -4 9 1607656799728402900 1003 -2 11 name: weather tags: area=南 time altitude humidity temperature ---- -------- -------- ----------- 1607604432455278300 1001 -5 10 1607656662027484500 1001 -5 11 1607656706278952000 999 -5 11 name: weather tags: area=西 time altitude humidity temperature ---- -------- -------- ----------- 1607656751612223600 1002 -2 11
6.聚合
①count()函數
返回一個(field)字段中的非空值的數量。
SELECT COUNT(<field_key>) FROM <measurement_name> [WHERE <stuff>] [GROUP BY <stuff>]
> select count(humidity) from weather name: weather time count ---- ----- 0 6
②MEAN() 函數
返回一個字段(field)中的值的算術平均值(平均值)。字段類型必須是長整型或float64。
語法格式:SELECT MEAN(<field_key>) FROM <measurement_name> [WHERE <stuff>] [GROUP BY <stuff>]
> SELECT MEAN(humidity) from weather name: weather time mean ---- ---- 0 -3.8333333333333335
③MEDIAN()函數
從單個字段(field)中的排序值返回中間值(中位數)。中值是在一組數值中居於中間的數值。字段值的類型必須是長整型或float64格式。
語法:SELECT MEDIAN(<field_key>) FROM <measurement_name> [WHERE <stuff>] [GROUP BY <stuff>]
> SELECT MEAN(humidity) from weather name: weather time mean ---- ---- 0 -3.8333333333333335
④SPREAD()函數
返回字段的最小值和最大值之間的差值。數據的類型必須是長整型或float64。
語法:
SELECT SPREAD(<field_key>) FROM <measurement_name> [WHERE <stuff>] [GROUP BY <stuff>]
> select spread(humidity) from weather name: weather time spread ---- ------ 0 3
⑤SUM()函數
返回一個字段中的所有值的和。字段的類型必須是長整型或float64。
語法:SELECT SUM(<field_key>) FROM <measurement_name> [WHERE <stuff>] [GROUP BY <stuff>
> select sum(humidity) from weather name: weather time sum ---- --- 0 -23
⑥INTEGRAL()函數
返回曲線
語法:SELECT INTEGRAL( [ * | <field_key> | /<regular_expression>/ ] [ , <unit> ] ) [INTO_clause] FROM_clause [WHERE_clause] [GROUP_BY_clause] [ORDER_BY_clause] [LIMIT_clause] [OFFSET_clause] [SLIMIT_clause] [SOFFSET_clause]
> select INTEGRAL(temperature) from weather name: weather time integral ---- -------- 0 497728.82358215
⑦distinc()函數
> select distinct(temperature) from weather name: weather time distinct ---- -------- 0 10 0 9 0 11
7.limit限制條數
#顯示一條信息 > select * from weather limit 1 name: weather time altitude area humidity temperature ---- -------- ---- -------- ----------- 1607604432455278300 1001 南 -5 10 #limit 10 offset 15,就是從第15行開始之后的10條數據 > select * from weather limit 2 offset 2 name: weather time altitude area humidity temperature ---- -------- ---- -------- ----------- 1607656662027484500 1001 南 -5 11 1607656706278952000 999 南 -5 11
8.or
influxDB中沒有in的操作,但是有or。對於習慣了mysql的in來說,用or就需要在代碼中循環了。
> select * from weather where altitude=1001 or temperature=11 name: weather time altitude area humidity temperature ---- -------- ---- -------- ----------- 1607656662027484500 1001 南 -5 11 1607656706278952000 999 南 -5 11 1607656751612223600 1002 西 -2 11 1607656799728402900 1003 東 -2 11
9.模糊查詢
> select * from test name: test time app count host monitor_name num ---- --- ----- ---- ------------ --- 1585897703920290000 1 127.0.0.1 test 1585897983909417000 ios 2 127.0.0.1 test1 3 1585898383503216000 ios 2 127.0.0.1 test1 3 1585901694441000000 ios 2 127.0.0.1 app1 3 1585901704179677000 ios 2 127.0.0.1 ios1 3 ## =~/給定字段/ 包含指定字段的 > select * from test where monitor_name =~/app/ name: test time app count host monitor_name num ---- --- ----- ---- ------------ --- 1585901694441000000 ios 2 127.0.0.1 app1 3 ##=~/^給定字段/ 以指定字段開始的 > select * from test where monitor_name =~/^app/ name: test time app count host monitor_name num ---- --- ----- ---- ------------ --- 1585901694441000000 ios 2 127.0.0.1 app1 3 ##=~/給定字段$/ 以指定字段結尾的 > select * from test where monitor_name =~/p1$/ name: test time app count host monitor_name num ---- --- ----- ---- ------------ --- 1585901694441000000 ios 2 127.0.0.1 app1 3
10.展示tag
> show tag keys from weather name: weather tagKey ------ altitude area
#查詢單個tag的value值 #查詢所以tag為altitude的value的值 > show tag values from weather with key="altitude" name: weather key value --- ----- altitude 1000 altitude 1001 altitude 1002 altitude 1003 altitude 999