0

我在 PHP 中有以下代码:

$IDs = implode(",", array_keys($result["matches"]));
$sql = "SELECT * FROM table WHERE id IN ($IDs)";

我正在将它“翻译”成 Node.js,我想 implode 变成了 array.join,所以我尝试了这个:

var ids = Object.keys(answer['matches']).join(",");
var sql = "SELECT * FROM xml_it WHERE id IN (" + ids + ")";

现在,如果我回显 PHP $sql 变量,我会得到预期的结果:

SELECT * FROM table WHERE id IN (3187,3220,3233,3245,3246,3251,3253,3256, ...

但是,使用 node.js,我得到了这个:

SELECT * FROM xml_it WHERE id_xml IN ([0,1,2,3,4, ...

answer['matches'] 显然是相同的结果(它基本上是 Sphinx 搜索结果)

知道为什么我没有将它作为字符串而是键吗?

$result 和 var answer 的数据如下所示:

[{"doc":3187,"weight":1,"attrs":{}},{"doc":3220,"weight":1,"attrs":{}},{"doc":3233,"weight":1,"attrs":{}},
4

1 回答 1

0

这应该有效:

var matches = answer['matches'].map( function( o ) {
  return o.doc;
});
var ids = matches.join(",");
var sql = "SELECT * FROM xml_it WHERE id IN (" + ids + ")";

由于结构不同,因此必须绘制 doc 字段

于 2012-09-12T16:12:52.400 回答