概述
使用语句WHERE
可以过滤数据,将符合特定条件的数据保留下来,将不满足条件的舍弃。
语法
WHERE <conditions>
详情
<condition>
:数据过滤条件。必要时,可使用逻辑运算符&&
和||
将过滤条件组合。逻辑运算结果为TRUE的数据才会保留。- 请注意,如果
<condition>
中引用了异源别名,则会计算其笛卡尔积。
示例图集
在一个空图集中,逐行运行以下语句,创建示例图集:
create().node_schema("student").node_schema("course").edge_schema("takes")
create().node_property(@*, "name").node_property(@student, "credits_gained", int32).node_property(@course, "credits", int32).node_property(@student, "age", int32)
insert().into(@student).nodes([{_id:"S1", name:"Jason", credits_gained:25, age:19}, {_id:"S2", name:"Lina", credits_gained:23, age:18}, {_id:"S3", name:"Eric", credits_gained:29, age:21}, {_id:"S4", name:"Emma", credits_gained:26, age:23}, {_id:"S5", name:"Pepe", credits_gained:24, age:18}])
insert().into(@course).nodes([{_id:"C1", name:"French", credits:4}, {_id:"C2", name:"Math", credits:5}, {_id:"C3", name:"Literature", credits:5}, {_id:"C4", name:"Art", credits:2}])
insert().into(@takes).edges([{_from:"S1", _to:"C1"}, {_from:"S2", _to:"C1"}, {_from:"S3", _to:"C1"}, {_from:"S2", _to:"C2"}, {_from:"S3", _to:"C2"}, {_from:"S4", _to:"C2"}, {_from:"S5", _to:"C2"}])
无笛卡尔积运算
find().nodes({@student}) as stu
where stu.credits_gained > 25
return stu.name
结果:
stu.name |
---|
Eric |
Emma |
find().nodes({@student}) as stu
where stu.credits_gained > 25 && stu.age > 21
return stu.name
结果:
stu.name |
---|
Emma |
有笛卡尔积运算
find().nodes({@student}) as stu
find().nodes({@course.name in ["Literature", "Art"]}) as newCrs
where stu.credits_gained + newCrs.credits > 30
return table(stu.name, newCrs.name, stu.credits_gained + newCrs.credits)
结果:
stu.name | newCrs.name | ADD(stu.credits_gained,newCrs.credits) |
---|---|---|
Eric | Art | 31 |
Eric | Literature | 34 |
Emma | Literature | 31 |