2017-05-31 86 views
0

可以用來創建方案試圖從一類更新/更新插入到另一個

CREATE CLASS v1 EXTENDS v 
CREATE CLASS v2 EXTENDS v 
CREATE PROPERTY v1.p1 STRING 
CREATE PROPERTY v1.p2 STRING 
CREATE PROPERTY v1.p3 STRING 
CREATE PROPERTY v2.p1 STRING (MANDATORY TRUE) 
CREATE PROPERTY v2.p2 STRING (MANDATORY TRUE) 
CREATE PROPERTY v2.p3 STRING 
CREATE INDEX v2.p1 UNIQUE 
INSERT INTO v1 (p1,p2,p3) VALUES ('a','b','c') 
INSERT INTO v1 (p1,p2,p3) VALUES ('d','e','f') 
INSERT INTO v1 (p1,p2,p3) VALUES ('h','i','j') 
INSERT INTO v2 (p1,p2,p3) VALUES ('a','y','z') 

和這些是以下中選擇的查詢的結果從

選擇下列V1

{ 
    "result": [ 
     { 
      "@type": "d", 
      "@rid": "#33:0", 
      "@version": 1, 
      "@class": "v1", 
      "p1": "a", 
      "p2": "b", 
      "p3": "c" 
     }, 
     { 
      "@type": "d", 
      "@rid": "#34:0", 
      "@version": 1, 
      "@class": "v1", 
      "p1": "d", 
      "p2": "e", 
      "p3": "f" 
     }, 
     { 
      "@type": "d", 
      "@rid": "#35:0", 
      "@version": 1, 
      "@class": "v1", 
      "p1": "h", 
      "p2": "i", 
      "p3": "j" 
     } 
    ], 
    "notification": "Query executed in 0.011 sec. Returned 3 record(s)" 
} 

選擇V2

{ 
    "result": [ 
     { 
      "@type": "d", 
      "@rid": "#41:0", 
      "@version": 1, 
      "@class": "v2", 
      "p1": "a", 
      "p2": "y", 
      "p3": "z" 
     } 
    ], 
    "notification": "Query executed in 0.013 sec. Returned 1 record(s)" 
} 

我已經嘗試了多個sql語句,嘗試將v1和v2的字段合併到近兩天,但無法找到答案。你可以告訴我要去的地方錯了

LET $t = SELECT FROM v1 
UPDATE v2 SET (p1,p2) VALUES ($t.$current.p1, $t.$current.p2) UPSERT WHERE $current.p1 = $t.$current.p1 

期望的結果將是V2有值「A,B,Z」和已添加了額外的兩個記錄,因爲他們在V1

更新當前記錄
+0

所以,我只想澄清,你想擁有,在結束,包含「abv」「de」「hi」的v2? –

+0

v2需要包含3條記錄 –

+0

v2需要包含3條記錄。記錄1(a,b,z),記錄2(d,e,f),記錄3(h,i,j) –

回答

1

這是一個JavaScript功能來做到這一點。

初步方案: enter image description here

JS功能:

var g = orient.getGraph(); 
var v1 = g.command("sql","SELECT FROM v1"); 

for(i=0; i<v1.length; i++){ 
    var check_index = g.command("sql","select from index:v2.p1 where key='"+v1[i].getProperty("p1")+"'"); 
    if(check_index.length>0){ 
    //v1 record already present in v2 index - UPDATE 
    print("#### "+"update "+check_index[0].getProperty("rid").getId()+" set p2='"+v1[i].getProperty("p2")+"'"+" ####"); 
    g.command("sql","update "+check_index[0].getProperty("rid").getId()+" set p2='"+v1[i].getProperty("p2")+"'"); 
    } else { 
    //v1 record not present in v2 index - INSERT 
    print("#### "+"insert into v2 set p1='"+v1[i].getProperty("p1")+"',p2='"+v1[i].getProperty("p2")+"',p3='"+v1[i].getProperty("p3")+"'"+" ####"); 
    g.command("sql","insert into v2 set p1='"+v1[i].getProperty("p1")+"',p2='"+v1[i].getProperty("p2")+"',p3='"+v1[i].getProperty("p3")+"'"); 
    } 
    g.commit(); 
} 

結果: enter image description here

+0

對不起,已經離開了幾天,會給這個嘗試 –

+0

這是完美的,非常感謝你 –

+0

嗨@JohnBrogan如果這對你有用,你能標記答案是正確的嗎?非常感謝你 :-) –