OrientDB: How to update column using select query

2020-04-10 14:03发布

问题:

I need to update a column in table using following:

update Table1 set name = (select productName from Table2 where
@rid=$parent.$current.productid)

Query works fine but instead of name query stores value in "[productname]" format.

I have read orientdb documentation, I guess select query returns result in collection format. so I have already tried following functions

  • get(1)
  • first()
  • [0] etc (my desperate attempt :)

Thanks in advance.

回答1:

I tried searching but did not get any clean ans, but i making following change worked for me & got the job done :)

update Table1 set name=(select productname from Table2 where
@rid=$parent.$current.productid), 
name= name.replace("\[","").replace("\]","")

Hope this saves time for someone.



回答2:

You observe this behavior since the sub query (the select query) always returns a collection. The LET block would help you here. Following is how you use the LET block in your query;

update Table1 set name = $name[0].productname LET $name = (select productname from Table2 where @rid=$parent.$current.productId)

The LET block is useful for sub queries, projections and holding results that will be used multiple times.

You can find more information here.

Hope this helps.



回答3:

Apparently your answer (@Omega Silva) does not work as it is.

This is the error I get

I have the same problem here and I haven't found an elegant solution.

I want to copy the @rid of Table2 into a field "r1" of Table1 where Table2.f1=Table1.f2 (f1 & f2 just two other fields).

The only solution I came up with is this:

UPDATE Table1 SET r1=(SELECT @rid FROM Table2 
WHERE Table2.f1=$parent.$current.f2)

And this returns a string with the entire field/value list. Then I trim it as follows:

UPDATE Table1 SET r1=r1.substring(6,12) 

(to keep just the @rid part)

But it seems to me that there has to be a better/more elegant solution, with a single query.

Any ideas?



回答4:

update Table1 set name=(select productname from Table2 where
@rid=$parent.$current.productid), 
name= name.replace("[","").replace("]","")


回答5:

Try that method (hierahical linking)

UPDATE test1 SET ttt=(SELECT FROM test1 WHERE id=$parent.$current.parentId),
parent=ttt[0].@rid,ttt=null

It is works on orientdb 2.19



标签: sql orientdb