前面介绍了使用insert实现新增和修改记录的使用姿势,接下来我们看一下另外一个简单的使用方式,如何删除数据
1. delete 语句
delete的官方语法如下
DELETE FROM <measurement_name> WHERE [<tag_key>='<tag_value>'] | [<time interval>]
delete语句和我们常见sql语法有点像,但是注意一下上面的where中的条件,只允许根据tag和时间来进行删除操作
下面给出几个简单的例子
case1 根据时间删除
> select * from add_test
name: add_test
time age boy email name phone user_id
---- --- --- ----- ---- ----- -------
1564149327925320596 19 true bangzewu@126.com YiHui 110 0
1564149920283253824 18 true bangzewu@126.com YiHui 110 21
1564150279123000000 18 true bangzewu@126.com YiHui 110 22
> delete from add_test where time>=1564150279123000000
> select * from add_test
name: add_test
time age boy email name phone user_id
---- --- --- ----- ---- ----- -------
1564149327925320596 19 true bangzewu@126.com YiHui 110 0
1564149920283253824 18 true bangzewu@126.com YiHui 110 21
case2 根据tag删除
注意name为保留名,因此需要用双引号括起来
> show tag keys from add_test
name: add_test
tagKey
------
name
phone
> delete from add_test where "name"='YiHui'
> select * from add_test
>
2. 不同保存策略的数据删除
从前面的语法定义中,没有看到指定保留策略的情况,那么如果需要删除某个保存策略的数据,应该怎样?
> insert add_test,name=YiHui,phone=110 boy=true,age=19i,user_id=2
> insert into "1D" add_test,name=YiHui,phone=110 boy=true,age=19i,user_id=1
> select * from add_test
name: add_test
time age boy name phone user_id
---- --- --- ---- ----- -------
1564483471390538399 19 true YiHui 110 2
> select * from "1D".add_test
name: add_test
time age boy name phone user_id
---- --- --- ---- ----- -------
1564483483748916258 19 true YiHui 110 1
> delete from add_test where "name"='YiHui'
> select * from add_test
> select * from "1D".add_test
>
执行上面的case之后,发现根据tag进行删除时,默认策略,和"1D"保存策略中的数据都被删除掉了
下面是另外一个验证
> select * from add_test;
name: add_test
time age boy name phone user_id
---- --- --- ---- ----- -------
1564483778197609864 19 true YiHui 110 1
> insert into "2_h" add_test,name=YiHui,phone=110 boy=true,age=19i,user_id=1
> select * from "2_h".add_test;
name: add_test
time age boy name phone user_id
---- --- --- ---- ----- -------
1564483793280811751 19 true YiHui 110 1
> delete from add_test where time=1564483793280811751
> select * from "2_h".add_test;
> select * from add_test;
name: add_test
time age boy name phone user_id
---- --- --- ---- ----- -------
1564483778197609864 19 true YiHui 110 1
我们在"2_h"
这个策略中新增了一条数据,直接根据时间进行删除,当前的策略下的数据没有影响,"2_h"
策略中刚添加的数据被删除掉了