n1ql查询where子句,如果争论为空或为空,则返回全部

时间:2019-06-14 01:59:18

标签: couchbase n1ql

如果在where子句中传递的参数为空或null,如何编写n1ql查询以返回记录?

[{
 "name": "John",
 "age": 12
},
{
 "name": "Smith",
 "age": 12
},
{
 "name": "Kevin",
 "age": 15
}]
@Query("SELECT * FROM #{#n1ql.bucket} " +
            "WHERE age = $1;")
List<Name> findByAge(Integer age);

预期: 如果年龄为空 返回

[{
 "name": "John",
 "age": 12
},
{
 "name": "Smith",
 "age": 12
},
{
 "name": "Kevin",
 "age": 15
}]

如果年龄为12 返回

[{
 "name": "John",
 "age": 12
},
{
 "name": "Smith",
 "age": 12
}]

1 个答案:

答案 0 :(得分:1)

尝试一下

SELECT *
FROM default
WHERE age = $1 OR IFMISSINGORNULL($1,"") = "";