Suppose I have table called tree with id, and parent-id and data are b-tree
1
2 3
4 5 6 7 8 9
etc
all these are store in tree table like
1 - null
2 - 1
3 - 1
4 - 2
5 - 2
6 - 2
7 - 3
8 - 3
9 - 3
etc
so please write Query to fetch child tree of 2
output should be like
4 - 2
5 - 2
6 - 2
xx - 4
etc..
Select id, parent_id from tablename where parent_id = 2. Perhaps you need bottom level ones? Then you need to left join the table with itself and only return the rows that don't have children.
Edit: if the maximum number of hierarchy levels is high or unknown, you should use hierarchyid: https://msdn.microsoft.com/en-us/library/bb677213.aspx
Hope this helps!
Related
I have a table which lists all the employees in an org with their immediate parent.
id
name
parent
type
1
A
0
1
2
B
0
1
3
C
1
2
4
D
2
2
5
E
3
3
6
F
4
3
7
E
5
4
8
F
6
4
I have another table which lists actions by last node employees:
id
action
type
emp_id
1
Action Name
0
7
2
Action Name
0
8
3
Action Name
1
7
4
Action Name
2
7
5
Action Name
3
8
6
Action Name
4
8
7
Action Name
5
7
8
Action Name
6
8
I need to show a hierarchical view of the action count. As evident above, all the actions are created by either employee with an id of 7 or 8. I need to show the number of actions by employees with type 1 based on their own actions or by any sub down the chain.
At the moment, I pull the counts on the end node using sql and then use the scripting language to build the hierarchy bottom up.
Is there a better way to achive this with SQL?
I am using 1x3 matrix to store data in mysql like below:-
ID parentID directID
1 0 0 <- This is first ID (So parent is no one),
2 1 1
3 1 1
4 - 3
5 - 4
6 1 1
7 2 1
8 2 1
9 2 1
10 6 1
11 6 2
I am working on a MLM (multi level marketing) project with 1x3 matrix plan.
As I am using 1x3 matrix structure, I have to find the child which doesnot has 3 child below it, given the top ID as 1 (1 is directID of new ID under which it has joined). The New ID that joins the team has to assign a parentID.
I have the following table (called node_items):
id node_id position
== ======= ========
1 1 1
2 1 1
3 2 1
4 2 1
5 2 1
6 2 1
7 3 1
8 3 1
9 3 1
10 3 1
The position field is supposed to mark an items position in the node. They are currently all set at 1. I am wanting to fix this table by setting an incremental position for each item per node_id. So, the result I am after is:
id node_id position
== ======= ========
1 1 1
2 1 2
3 2 1
4 2 2
5 2 3
6 2 4
7 2 5
8 3 1
9 3 2
10 3 3
So I somehow need to group each item by node_id and set the position to an initial value of 1 and increment it for each subsequent item with that node_id.
Note: To keep my explanation simple, the above node_ids are shown in order (1,1, 2, 2,2,2,2,3,3,3), but this isn't usually the case.
What query can I use to update the position values incrementally for each node_id?
Fixing the table once can be done with an UPDATE:
SET #n = 0, #p = 1;
UPDATE node_items
SET position = (#p := IF(node_id=#n, #p+1, 1)),
node_id = (#n := node_id)
ORDER BY node_id, id;
Making the table maintain the position values as you insert/update/delete data is harder. Basically, can't do it while allowing concurrent updates. You have to lock the table in every session that needs to do writes to the table. This makes concurrent sessions run serially.
You can read my old post about this here: Some sort of “different auto-increment indexes” per a primary key values
![enter image description here][1]In the First Table tblserialnumbersprimary i have a primary field called as serialNoId ,this field is repeating multiple times in the second table tblserialnumbers. when we are inserting multiple records with that serialNoId.
I want to get Distinct records from the first table i.e tblserialnumbersprimary and corresponding multiple records from the dependent table i.e tblserialnumbers in MySQL
First Table Fields are:
tblserialnumbersprimary (serialNoId,serialPO,serialProductNo,
SerialNumberMode,serialNoAutoPrefix,serialDateOfCreation,
serialModifiedBy,serialStatus)
Second Table Fields are:
tblserialnumbers(serialId,serialNoId,
serialNo,serialNoBatchId![enter image description here][1])
I tried With This Joins Query.But its giving multiple records of first table
select * FROM tblserialnumbersprimary
LEFT OUTER JOIN tblserialnumbers
ON (tblserialnumbersprimary. serialNoId = tblserialnumbers.serialNoId )
First Table values are:
serialNoId serialPO serialProductNo SerialNumberMode serialNoAutoPrefix serialDateOfCreation serialModifiedBy serialStatus
1 PO1 PROD121 Automatic TCS-03 2/25/2014 12:00:00 AM admin 0
2 PO2 PROD345 Automatic TCS-03 2/25/2014 12:00:00 AM admin 1
3 PO5 PROD816 Automatic 2/26/2014 12:00:00 AM admin 1
4 PO1 PROD121 Automatic GTS-03 2/26/2014 12:00:00 AM admin 1
Second Table values are:
serialId serialNoId serialNo serialNoBatchId
1 1 TCS-03-PROD121-1 batch1
2 1 TCS-03-PROD121-2
3 1 TCS-03-PROD121-3 batch3
4 1 TCS-03-PROD121-4
5 1 100
6 1 101
1 2 TCS-03-PROD345-1 batch1
2 2 TCS-03-PROD345-2
3 2 TCS-03-PROD345-3 batch3
4 2 TCS-03-PROD345-4
1 3 --1
2 3 --2
3 3 --3
4 3 --4
5 3 12
6 3 13
7 3 11
1 4 -PROD816-1 batch1
2 4 -PROD816-2 batch2
1 5 GTS-03-PROD121-1 batch1
2 5 GTS-03-PROD121-2
3 5 GTS-03-PROD121-3 batch3
4 5 GTS-03-PROD121-4
Use alias and join for record in multiple table.
For i.e. `SELECT table2.id,
table1.pid,
table1.color,
table2.pname,
table2.image
FROM tbl_productcolor table1
JOIN tbl_product table2 ON table1.pid = table2.id;`
Apply your table in this way you can get all the data.
I have two tables:
Part: id, partnumber
Alternative: partid, alternativepartid
Let's say that Part contains the following records:
1 - part 1 2
2 - part 2
3 - part 3
4 - part 4
5 - part 5
And Alternative contains the following records:
1 - 2
1 - 3
2 - 4
3 - 5
I would like to create a query to get all parts that are related to a given id.
In my case that would mean that if I query for part 1 (id 1) I would like to find:
2 and 3 but also 4 and 5 as 4 is an alternative to 2 and 5 is an alternative to 3.
When I query for 3, I would like to find: 5, 1, 2, and 4
Does this make sense?
Would it be possible to retrieve the complete set of records in one query?
Thanks!