OrientDB: how to update a column using select query
I need to update a column in a table using the following command:
update Table1 set name = (select productName from Table2 where
@rid=$parent.$current.productid)
Query works fine, but instead of querying a name it stores the value in the format [[computername] ".
I read orientdb documentation, I think selecting a query returns the result as a collection. so i have already tried the following functions
- receive (1)
- the first()
- [0] etc. (my desperate attempt :)
Thanks in advance.
source to share
You observe this behavior because the sub query (select query) always returns a collection. The LET block will help you here. Below is an example of using the LET block in your request;
update Table1 set name = $name[0].productname
LET $name = (select productname from Table2 where @rid=$parent.$current.productId)
The LET block is useful for subqueries, forecasts, and results that will be used multiple times.
You can find more information here .
Hope it helps.
source to share
Apparently your answer (@Omega Silva) doesn't work as it is.
This is the error I am getting
I have the same problem and have not found an elegant solution.
I want to copy the @rid of table2 to field "r1" of table 1 where Table2.f1 = Table1.f2 (f1 and f2 are just the other two fields).
The only solution I have come up with is the following:
UPDATE Table1 SET r1=(SELECT @rid FROM Table2
WHERE Table2.f1=$parent.$current.f2)
And this returns a string with the entire field / value. Then I crop it like this:
UPDATE Table1 SET r1=r1.substring(6,12)
(to keep only the @rid part)
But it seems to me that there should be a better / more elegant single query solution.
Any ideas?
source to share