recursive-query

Get all children by parent id and where clause in mysql

ぃ、小莉子 提交于 2020-01-30 11:45:26
问题 I have a table that stores id and parent_id in same table. I want a recursive query that accepts parent_id as an argument and returns all child nodes with nth level. For this I am using this code and working properly for me. select id, name, parent from (select * from tablename order by parent, id) tablename, (select @pv := '1') initialisation where find_in_set(parent, @pv) > 0 and @pv := concat(@pv, ',', id) My problem start from here: I want to add WHERE clause with result set but unable to

Get all children by parent id and where clause in mysql

社会主义新天地 提交于 2020-01-30 11:42:08
问题 I have a table that stores id and parent_id in same table. I want a recursive query that accepts parent_id as an argument and returns all child nodes with nth level. For this I am using this code and working properly for me. select id, name, parent from (select * from tablename order by parent, id) tablename, (select @pv := '1') initialisation where find_in_set(parent, @pv) > 0 and @pv := concat(@pv, ',', id) My problem start from here: I want to add WHERE clause with result set but unable to

SQL query to get the list of supervisor hierarchy. employee --> supervisor --> supervisor

久未见 提交于 2020-01-30 08:13:05
问题 I have two tables Employee and Department this image shows the manager of every employee. I want to write a SQL query that gives me a list of all the supervisor (Manager, Manager of Manager..). I just want a single column that displays a list of supervisor when given a particular employee. E.g. If I give employee id = 202 then I should receive 200,130 |supervisor | +-----------+ | 200 | | 130 | I have this query WITH emp_dept as( SELECT employee_id,manager_id FROM employee,department WHERE

Laravel pass parameters from controller to Model using 'With' clause

浪尽此生 提交于 2020-01-25 16:40:10
问题 I am new in Laravel, I want to pass the the $id from my controller in the model using with clause My model class Menucategory extends Model { protected $fillable = ['title', 'parent_id', 'restaurant_id']; // loads only direct children - 1 level public function children() { return $this->hasMany('App\Menucategory', 'parent_id'); } // recursive, loads all descendants public function childrenRecursive() { return $this->children()->with('childrenRecursive'); } } My Controller public function show

Select Consecutive Numbers in SQL

自闭症网瘾萝莉.ら 提交于 2020-01-24 15:16:05
问题 This feels simple, but I can't find an answer anywhere. I'm trying to run a query by time of day for each hour. So I'm doing a Group By on the hour part, but not all hours have data, so there are some gaps. I'd like to display every hour, regardless of whether or not there's data. Here's a sample query: SELECT DATEPART(HOUR, DATEADD(HH,-5, CreationDate)) As Hour, COUNT(*) AS Count FROM Comments WHERE UserId = ##UserId## GROUP BY DATEPART(HOUR, DATEADD(HH,-5, CreationDate)) My thought was to

Recursive query

旧城冷巷雨未停 提交于 2020-01-24 12:37:13
问题 I have a table which contains the following fields Supervisorid Empid This is just like a referral program. A guy can refer 3 guys under him i.e, 3 is referring three guys namely 4 5 8 similarly 4 is referring 9 10 and 11 likewise 8 is referring 12, 13 it goes like this.. I want a query to get all EmpId under Supervisor 3 回答1: Do you want us to write the solution for you, or explain a bit how recursive queries can be built up ? An example of how they are built up is on http://publib.boulder

How can I traverse a tree bottom-up to calculate a (weighted) average of node values in PostgreSQL?

落花浮王杯 提交于 2020-01-23 01:07:07
问题 The typical example for e.g. summing a whole tree in PostgreSQL is using WITH RECURSIVE (Common Table Expressions). However, these examples typically go from top to bottom, flatten the tree and perform an aggregate function on the whole result set. I have not found a suitable example (on StackOverflow, Google, etc.) for the problem I am trying to solve: Consider an unbalanced tree where each node can have an associated value. Most of the values are attached to leaf nodes, but the others may

how to select all children and grand children of a node in sql / oracle? [duplicate]

吃可爱长大的小学妹 提交于 2020-01-17 07:22:11
问题 This question already has answers here : Recursive query in Oracle (2 answers) Closed 2 years ago . For instance if I have following table with this particular data: Table X Node_ID ParentNode_ID ---- ---- 1 - 2 1 3 1 4 2 5 2 6 4 7 5 8 6 9 8 and I need a query to select children and grand children (and grand grand children...) of node '2', meaning following result: children_of_node2 ------ 4 5 6 7 8 9 how can I do it with a select query and not using functions or declaring variables in oracle

How to traverse a path in a table with id & parentId?

邮差的信 提交于 2020-01-17 03:07:06
问题 Suppose I have a table like: id | parentId | name 1 NULL A 2 1 B 3 2 C 4 1 E 5 3 E I am trying to write a scalar function I can call as: SELECT dbo.GetId('A/B/C/E') which would produce "5" if we use the above reference table. The function would do the following steps: Find the ID of 'A' which is 1 Find the ID of 'B' whose parent is 'A' (id:1) which would be id:2 Find the ID of 'C' whose parent is 'B' (id:2) which would be id:3 Find the ID of 'E' whose parent is 'C' (id:3) which would be id:5

MySQL recursive-query replacement in PHP

时间秒杀一切 提交于 2020-01-17 01:36:08
问题 Since it looks like recursive queries aren't possible in MySQL, I am wondering if there is a solution to get the same information that also limits the number of queries I make to the database. In my case I have what amounts to a tree and given a node, I make a path back to the root and save the name of the nodes as I go. Given a table like this: id | parent ------------- 1 | 2 | 1 3 | 1 4 | 2 5 | 2 6 | 5 I want to select all ids on the path from 6 back to 1 (6,5,2,1). Since the total length