I have a simple graph with one parent and three children:
Querying for the children, I also get back the parent:
select name
from (
traverse in()
from (
select
from group
where name = 'Parent'
)
)
Results:
name
Parent
Child 1
Child 2
Child 3
How can I exclude the parent from the results in the query? I'd rather not process the results in my application code.
Thanks.
To get only the children name, I suggest a query like this:
select in('belongsTo').name as Name from Group where name = "Parent" unwind Name
Excluding where depth is zero seems to do the trick:
select name
from (
traverse in()
from (
select
from group
where name = 'Parent'
)
)
where $depth > 0
Results in:
name
Child 1
Child 2
Child 3
Related
I'm using SQLite and have two tables:
Line {
Element_ID,
length
}
Element{
Element_ID,
Name
}
I want to update the parameter "length" to a certain value in Line by specifying the "Name" in Element where Element_ID is the same for Line and Element.
SQLite does not support the JOIN statement in combination with the UPDATE statement, so I've been trying various combinations of UPDATE with EXISTS without any success. Example; update length to 2 for the line with element Name 'c18':
UPDATE Line
SET length = 2
WHERE EXISTS (SELECT length FROM Line WHERE Line.Element_ID = Element.Element_ID AND Element.Name = 'c18')
Result: Error: No such column: Element.Element_ID
UPDATE Line
SET length = 2
WHERE EXISTS (SELECT length FROM Line INNER JOIN Element ON Line.Element_ID = Element.Element_ID WHERE Element.Name = 'c18')
Result: The code updates length for ALL lines (it looks like it disregards the last part in the EXISTS-statement WHERE Element.Name = 'c18'. I don't understand why this is happening, because if I only run the SELECT-statement inside the WHERE EXISTS(..) the program selects the correct line (c18).
Can anyone help me with this?
The first query doesn't work because you are referencing Element in the WHERE clause without defining it anywhere (as a table or an alias).
The second one because the subquery does not depend on the main query (it is not a correlated subquery) and therefore it has the same value for every row.
A simpler way to do this could be :
UPDATE Line SET length = 2
WHERE Element_ID in ( SELECT Element_ID FROM Element WHERE Name = 'c18' )
or to make it a bit more explicit :
UPDATE Line SET length = 2
WHERE Element_ID in ( SELECT e.Element_ID FROM Element e WHERE e.Name = 'c18' )
I have simple graph and i want to find category and delete it. If category has children, than delete category and all children. I've made deletion category with children with this command:
MATCH path = (c:Category)-[*]->(cc:Category)
WHERE c.id = "95bec604-5da2-4297-b792-5a866e292df4"
DETACH DELETE path
But this comamnd does not work for single node, without children. How i can achieve it?
Add a 0 as the lower bound for the variable-length pattern. By default it's 1, but when it's 0 (provided the label used for the end node is also on the start node) this will allow it to match even when there are no relationships from the start node.
MATCH path = (c:Category)-[*0..]->(cc:Category)
WHERE c.id = "95bec604-5da2-4297-b792-5a866e292df4"
DETACH DELETE path
In general, this approach would work if you want to delete the entire path:
MATCH path = ( some path )
FOREACH (node IN nodes(path) |
DETACH DELETE node
)
Imagine a simple table that defines a tree structure.
create table nodes (
id integer primary key,
name text not null,
parent integer
)
Some example nodes:
Node 1 is parent of 2 and 3. Node 3 is parent of 4. Is it possible to write a SQL query in SQLite, so that it returns:
id path
1 foo
2 foo/bar
3 foo/baz
4 foo/baz/stuff
You can perform recursion in SQLite using recursive common table expressions.
An example query that would return the node paths:
with recursive paths(id, name, path) as (
select id, name, name from nodes where parent is null
union
select nodes.id, nodes.name, paths.path || '/' || nodes.name
from nodes join paths where nodes.parent = paths.id
)
select id, path from paths
I'm new in SQLite and I would like to ask if there's a way use SELECT as expression of CASE WHEN?
e.g.
CASE WHEN item_type = 'item' THEN
SELECT * FROM items ... END AS 'col_1',
WHEN item_type = 'subitem' THEN
SELECT * FROM subitems ... END AS 'col_2'
Yes you can. All you need to do is to enclose your sub-selects in parentheses. For example, this works:
SELECT CASE WHEN 1 = 0 THEN (SELECT 1) ELSE (SELECT 0) END
Also, sub-select must return exactly single value (it cannot return more than 1 value). So, if you tried to SELECT * FROM subitems ..., it would NOT work, unless table subitems contained only one column and single row in it.
I am trying to create "previous/next node" navigation on my nodes in order to show 2 previous and 2 following nodes in the term currently being viewed. Here is the code that displays 2 prev and 2 next nodes, but it is not taxonomy aware, i. e. it sorts nodes according to their IDs:
Prev/Next node navigation with a thumbnail in a full node
If I add a node in the term after some time, it will display this node as the last one, not as a "neighbour" of a node uploaded e.g. 3 months ago.
I have tried with "n.title", but it doesn't change anything. Ideally, it should order them either by titles or url aliases.
Thank you in advance!
it's not querying the taxonomy tables in the database query. You probably want to add a variable to the function as such dad_prev_next($current_node = NULL, $op = 'p', $tid) to pass it the term ID and then add that to your query through an inner join
SELECT n.nid, n.title
FROM {node} n
INNER JOIN {taxonomy_index} t
ON n.nid = t.nid
WHERE n.nid $sql_op :nid
AND t.tid = :tid
AND type IN ('photos')
AND status = 1
ORDER BY n.nid $order
LIMIT 1
I think that should be pretty close, then just pass that at the end of db_query db_query($sql, array(':nid' => $current_node -> nid, ':tid' => $tid));
Newer version of Previous/Next module has multiple options for sorting prev/next nodes. You can use the workaround to get thumbnails too:
http://drupal.org/project/prev_next
http://drupal.org/node/1790290