如何在ydn-db中实现内连接?

时间:2014-11-20 09:42:09

标签: indexeddb ydn-db

我需要根据order_header在两个表order_itemORDER_ID上实现内部联接。等效的SQL查询: SELECT * FROM order_header INNER JOIN order_item ON order_header.ORDER_ID=order_item.ORDER_ID如何在ydn-db中实现这一点?

1 个答案:

答案 0 :(得分:0)

使用db.scan进行表格加入。目前YDN-DB没有很好的加入包装,但manual joining并不那么困难。这是加入SELECT * FROM Supplier, Part WHERE Supplier.CITY = Part.CITY的查询。

var iter_supplier = new ydn.db.IndexValueIterator('Supplier', 'CITY');
var iter_part = new ydn.db.IndexValueIterator('Part', 'CITY');
var req = db.scan(function(keys, values) {
  var SID = keys[0];
  var PID = keys[1];
  console.log(SID, PID);
  if (!SID || !PID) {
    return []; // done
  }
  var cmp = ydn.db.cmp(SID, PID); // compare keys
  if (cmp == 0) {
    console.log(values[0], values[1]);
    return [true, true]; // advance both
  } else if (cmp == 1) {
    return [undefined, SID]; // jump PID cursor to match SID
  } else {
    return [PID, undefined]; // jump SID cursor to match PID
  }
}, [iter_supplier, iter_part]);

详情请见Join query article