[英]Neo4J COALESCE in relationship
我正在尝试创建关系,但由于某种原因,我无法在我的关系查询中使用合并。 我完全卡住了,其中一些可能格式不正确,但我试图一步一步地修复它。 这是我的代码
/* Get the kill assassin and student by relation UUID*/
'MATCH (a:Student)-[r:TARGET]->(t:Student) WHERE r.uuid = $uuid ' +
/* Lets Kill the student and set confirmed to true */
'SET r.confirmed = true, t.IsDead = true WITH ' +
/* Delete any teachers that may have the target of the student*/
'MATCH (t)<-[tr:TARGET]-(:Teacher) DELETE r ' +
/* Get the assassinated persons target. Set them as X. */
'MATCH (t)-[ar:TARGET]->(x:Student) WHERE ar.confirmed = false AND x.IsDead = false ' +
/* Lets deal with anyone who inherited our target if we are dead :(*/
'OPTIONAL MATCH (t)<-[sr:TARGET]-(ss:Student) WHERE sr.confirmed = false AND ss.IsDead = false ' +
/* Lets steal their kill node and set them as our new target unless of course someone decided to kill us and nab them */
'CREATE (COALESCE(t,a))-[nr:TARGET {killed:false,confirmed:false}]->(t) '
编辑:我清理了我的代码很多。 只是为了记录 coalesce 不能在关系中使用。 这是那些想知道的人的清理代码。
/**
* a: The student who made the kill
* v: The victim of the kill
* r: the relationship between a and t
* x: the victims target
* tr (OPTIONAL) : the teacher who we hired to kill our target
* ss: The person who will be inheriting t(victim)'s target
*/
/* Get the Killer, set them as A AND set the Victim as T */
'MATCH (a:Student)-[r:TARGET {uuid:$uuid,confirmed:false}]->(v:Student),' +
/* Fetch the Victims Target and set them as x */
'(v)-[:TARGET {confirmed:false}]->(x:Student {IsDead:false}) ' +
/* Check if we hired a teacher to assassinate our target. Set the relationship to TR (teacher relation)*/
'OPTIONAL MATCH (v)<-[tr:TARGET]-(:Teacher) ' +
/* Fetch the alive person who has the target (in-case we died and then it went into review) */
'MATCH (v)<-[sr:TARGET {confirmed:false}]-(ss:Student {IsDead:false}) ' +
/* Create a relationship between SS and t */
'CREATE (ss)-[:TARGET {killed:false,confirmed:false}]->(x) ' +
/* Set the Kill Relationship to complete and kill the victim */
'SET r.confirmed = true, v.IsDead = true,a.Balance = a.Balance + 3 ' +
/* Delete the teacher relationship */
'DELETE tr ', {uuid:uuid}
您不能在CREATE
使用表达式 - 但您可以将该表达式移动到WITH
语句中以将其分配给一个变量,然后在您的创建中使用该变量。
最后一行可以替换为:
/* Lets steal their kill node and set them as our new target unless of course someone decided to kill us and nab them */
WITH COALESCE(t, a) as n, t
CREATE (n)-[nr:TARGET {killed:false,confirmed:false}]->(t)
哪个编译得很好,但是我必须先修复查询中的其他一些问题:
SET r.comfirmed...
以WITH
结尾,但后面没有变量MATCH (t)<-...
以DELETE
结尾,但下一行以MATCH
开头 - 您需要在两者之间WITH
所以我最终得到的查询是:
MATCH (a:Student)-[r:TARGET]->(t:Student) WHERE r.uuid = $uuid
/* Lets Kill the student and set confirmed to true */
SET r.confirmed = true, t.IsDead = true
/* Delete any teachers that may have the target of the student*/
WITH a, t, r
MATCH (t)<-[tr:TARGET]-(:Teacher) DELETE r
/* Get the assassinated persons target. Set them as X. */
WITH a, t
MATCH (t)-[ar:TARGET]->(x:Student) WHERE ar.confirmed = false AND x.IsDead = false
/* Lets deal with anyone who inherited our target if we are dead :(*/
OPTIONAL MATCH (t)<-[sr:TARGET]-(ss:Student) WHERE sr.confirmed = false AND ss.IsDead = false
/* Lets steal their kill node and set them as our new target unless of course someone decided to kill us and nab them */
WITH COALESCE(t, a) as n, t
CREATE (n)-[nr:TARGET {killed:false,confirmed:false}]->(t)
但即便如此,它看起来也是错误的COALESCE
总是会解析为t
因为在查询中达到那么远的t
必须有一个值,所以最后的CREATE
只会在t
和它自身之间创建一个关系。 另外: OPTIONAL MATCH
引入了新变量ss
但该变量没有在其他任何地方使用(使可选匹配有些多余)。
我猜COALESCE
应该在ss
和a
之间,而不是t
和a
但很难说。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.