5.InfluxDB-InfluxQL基礎語法教程--WHERE子句

本文翻譯自官網,官網地址:(https://docs.influxdata.com/influxdb/v1.7/query_language/data_exploration/)sql

WHERE子句

語法:express

SELECT_clause FROM_clause WHERE <conditional_expression> [(AND|OR) <conditional_expression> [...]]

:在WHERE子句中,支持在fields, tags, and timestamps上進行條件表達式的運算。
:在InfluxDB的WHERE子句中,不支持使用 OR 來指定不一樣的time區間,以下面的sql將會返回空:less

SELECT * FROM "absolutismus" WHERE time = '2016-07-31T20:07:00Z' OR time = '2016-07-31T23:07:17Z'

Fields

語法以下:ui

field_key <operator> ['string' | boolean | float | integer]

在WHERE子句中,支持對string, boolean, float 和 integer類型的field values進行比較。翻譯

注意在WHERE子句中,若是是string類型的field value,必定要用單引號括起來。若是不適用引號括起來,或者使用的是雙引號,將不會返回任何數據,有時甚至都不報錯! code

WHERE支持的運算符以下:orm

Operator Meaning
= equal to
<> not equal to
!= not equal to
> greater than
>= greater than or equal to
< less than
<= less than or equal to

支持的更多運算符詳見: Arithmetic Operations, Regular Expressionsblog

Tags

語法以下:flux

tag_key <operator> ['tag_value']

對於在WHERE子句中的tag values,也要用單引號括起來。若是不用引號括起來,或者使用雙引號,則查詢不會返回任務數據。甚至不會報錯。ci

Tag支持的運算符以下:

Operator Meaning
= equal to
<> not equal to
!= not equal to

還支持正則運算: Regular Expressions

Timestamps

對於大部分的SELECT 語句來講,默認的時間區間是1677-09-21 00:12:43.145224194 到 2262-04-11T23:47:16.854775806Z UTC.
對於有GROUP BY time() 的SELECT 語句,默認的時間區間是1677-09-21 00:12:43.145224194 UTC 到 now()。

Time Syntax 小節將會介紹如何在WHERE子句中指定時間 區間。


WHERE示例sql

  1. Select data that have specific field key-values

  2. Select data that have a specific string field key-value

    InfluxQL requires single quotes around string field values in the WHERE clause.

  3. Select data that have a specific field key-value and perform basic arithmetic

  4. Select data that have a specific tag key-value

    InfluxQL requires single quotes around tag values in the WHERE clause.

  5. Select data that have specific field key-values and tag key-values

    The WHERE clause supports the operators AND and OR, and supports separating logic with parentheses.

  1. Select data that have specific timestamps

    該sql將查詢h2o_feet中在7天之內的全部數據

關於WHERE語句的常見疑問

問題 :where子句查詢意外地未返回任何數據。

:一般狀況,出現該問題是由於在WHERE子句中沒有對tag values或string類型的field values使用單引號括起來的緣故。對於WHERE子句中的tag values或string類型的field values,若是沒有用引號括起來,或者是用的雙引號,這種時候,查詢不會返回任何結果,有時甚至也不會報錯。
在下面的示例sql中,對tag value的引號使用作說明。第一個sql沒有對tag value使用引號,第二個sql對tag value使用了雙引號,第三個sql則對tag value使用了單引號。能夠看到,第一和第二個sql都沒有返回任何查詢結果,而第三個sql返回了預期中的結果。

在下面的sql對string類型的field value的引號狀況作說明,其中field value爲「at or greater than 9 feet」。第一個sql沒有對field value使用引號,第二個sql對field value使用了雙引號,第三個sql則對field value使用了單引號。能夠看到,第一個sql報錯了,由於field valus中包含了空格。第二個sql雖然沒報錯,可是查詢結果爲空。第三個sql返回了預期中的結果。

相關文章
相關標籤/搜索