2

我正在尝试查询我的 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';
4

2 回答 2

3

如果 name 是一个聚类键,那么您可以像这样使用“in”和“allow filtering”进行查询:

select * from table_name where name in ('ukcust1','ukcust2','ukcust3') allow filtering

假设 name 不是集群键,如果符合逻辑,您可以使用集群键(例如 date_of_birth)——也就是说,如果按日期过滤对于名称来说是有意义的——就像这样:

select * from table_name where date_of_birth in (1969, 1972) name in ('ukcust1','ukcust2','ukcust3') allow filtering

如果你不能做这些事情中的任何一个,你将需要用Javascript(例如,foreach)循环遍历数组。

于 2018-05-14T20:54:32.300 回答
0

查询参数的正确输入是一个值数组。在这种情况下,它将是一个包含单个项目的参数数组,即名称数组。

const arr = ["ukcust1","ukcust2","ukcust5"];
const query = "SELECT * FROM table_name WHERE name = ?";

// Note the array containing a single item
const parameters = [ arr ];

client.execute(query, parameters, { prepare: true }, callback);

在文档中查看更多信息:https ://docs.datastax.com/en/developer/nodejs-driver/3.5/faq/#how-can-i-use-a-list-of-values-with-the- in-operator-in-a-where 子句

于 2018-05-16T09:37:42.100 回答