我正在尝试查询我的 cassandra 数据库以从阵列服务器端保存的名称列表返回数据。这是作为一个数组保存的。 我知道我正在访问的数据作为字符串存储在我的数据库中,因此我在它周围附加了单引号(我尝试过使用和不使用它,但没有运气)。
这是我的查询。
const arr = ["ukcust1","ukcust2","ukcust5"];
//Here I append single quotes before and after to each string if needed
const query = "SELECT * FROM table_name WHERE name = ?";
client.execute(query, arr, { prepare:true }, function (err, result) {
..//Code
};
我在这里缺少什么?我希望查询是:
SELECT * FROM table_name WHERE name = each of the names in the array 'arr';
最佳答案
如果 name 是集群键,那么您可以使用“in”和“允许过滤”进行查询,如下所示:
select * from table_name where name in ('ukcust1','ukcust2','ukcust3') 允许过滤
假设名称不是集群键,您可以使用集群键(例如,date_of_birth),如果它具有逻辑意义 - 也就是说,如果按日期过滤相对于名称有意义 - 如下所示:
select * from table_name where date_of_birth in (1969, 1972) name in ('ukcust1','ukcust2','ukcust3') 允许过滤
如果您无法执行上述任何操作,则需要使用 Javascript(例如 foreach)循环遍历数组。
关于node.js - 使用数组查询 cassandra 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50330801/