mysql parent and child with level - mysql

I have table structure like
id | parent_id
----------
1 0
2 0
3 0
4 0
5 1
6 1
7 2
8 2
9 5
10 7
This table has unlimited parent child relation
I want the end result as given below
id | parent_id | level
---------------------------
1 0 0
2 0 0
3 0 0
4 0 0
5 1 1
6 1 1
7 2 1
8 2 1
9 5 2
10 7 2
Can anybody help with suggestion?

Refer the following article:
http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/
From: The Nested Set Model
To: Finding the Depth of the Nodes
You shall arrive at the complete solution :)

Related

Find record less than or equals 0 but no repeat record

I have 2 tables, 1 first look this:
Table state_inventary
ID_STATE_INVENTARY, DESCRIPTION
0 STORE
1 TRANSIT
2 SOLD_STORE
3 STORAGE
Table article_stock
ID_STOCK ID_ORIGIN ARTICLE UNIT_SOLD ID_STATE_INVENTARY
0 1 A 10 0
1 2 A 0 1
2 1 B 5 2
3 3 C 0 3
4 4 D 0 3
5 5 E 10 1
6 2 A 0 2
7 1 B 0 2
I need to find articles with ID_STATE_INVENTORY with value 0 or 2 or 3, I get it
But I need to find articles with the UNIT_SOLD the sum is zero, I don't know how do these
I want to find somthing like that
ID_STOCK ID_ORIGIN ARTICLE UNIT_SOLD ID_STATE_INVENTARY
3 3 C 0 3
4 4 D 0 3
OR
ARTICLE
C
D
In my query I have next result
ID_STOCK ID_ORIGIN ARTICLE UNIT_SOLD ID_STATE_INVENTARY
1 2 A 0 1
3 3 C 0 3
4 4 D 0 3
6 2 A 0 2
7 1 B 0 2
Anyone idea how can I do?
Try this.
SELECT SUM(`UNIT_SOLD`) AS `UNIT_SOLD`, `ARTICLE` FROM `table_name` GROUP BY `ARTICLE`;

How can I find a hierarchy from a closure table in MYSQL if I have a list of names within the hierarchy?

Fiddle: http://sqlfiddle.com/#!9/c1495/1
I have a table named "locations":
id name parent
1 Sweden 0
2 England 0
3 Stockholm 1
4 Vasteras 1
5 Town 3
6 Town 4
7 London 2
8 Town 7
And table named "closure":
parent child depth
1 1 0
1 3 1
1 4 1
1 5 2
1 6 2
2 2 0
2 7 1
2 8 2
3 3 0
3 5 1
4 4 0
4 6 1
5 5 0
6 6 0
7 7 0
7 8 1
8 8 0
Now I'd like to convert a hierarchy of names I have on hand into location IDs.
Example: Sweden => Stockholm => Town would yield:
id name parent
1 Sweden 0
3 Stockholm 1
5 Town 3
How could I do this without also returning any of the places named "town", but where the parents aren't exactly the same?

MYSQL Stored procedure, approvals and approvers

Im kinda new reading stored procedure.
I was thinking if this is posible to do in store procedure in mysql.
I have sequence of approval process called step. approved column; 1 is yes 0 is no.
Basically I have Step 1 to 3..in my approval sequence.
if step 1 approved status is 0 he will be the first to approved or see the table.
if step 1 approve is 1. step 2 can now see the table.
Transaction Steps Table:
id transaction_id approver_id step approved
1 1 1 1 1
2 1 2 2 0
3 1 3 3 0
4 2 3 1 1
5 2 1 2 1
6 2 2 3 0
7 3 2 1 0
8 3 3 2 0
9 3 1 3 0
10 4 1 1 1
11 4 3 2 0
12 4 2 3 0
Example If my Approval id = 2
In My View:I can only see all those next in que approvals
id transaction_id approver_id step approved
2 1 2 2 0
6 2 2 3 0
7 3 2 1 0
pls let me know if this is possible. thank you
If I understand correctly, you want rows that are the first non-approved for each transaction and the approver is 2.
Try this:
select ts.*
from transactionsteps ts join
(select transaction_id, min(step) as minstep
from transactionsteps
where approved = 0
group by transaction_id
) t
on ts.transaction_id = t.transaction_id and
ts.step = t.minstep
where approver_id = 2;

retrieve integer name in shortest.path function of igraph

First, I have a shortest path matrix generated with igraph (shortest path)
When I want to retreive the node names with "get.shortest.path" it just brings me the number of each column and not its name:
[,a] [,b] [,c] [,d] [,e] [,f] [,g] [,h] [,i] [,j]
[a,] 0 1 2 3 4 5 4 3 2 1
[b,] 1 0 1 2 3 4 5 4 3 2
[c,] 2 1 0 1 2 3 4 5 4 3
[d,] 3 2 1 0 1 2 3 4 5 4
[e,] 4 3 2 1 0 1 2 3 4 5
[f,] 5 4 3 2 1 0 1 2 3 4
[g,] 4 5 4 3 2 1 0 1 2 3
[h,] 3 4 5 4 3 2 1 0 1 2
[i,] 2 3 4 5 4 3 2 1 0 1
[j,] 1 2 3 4 5 4 3 2 1 0
then:
get.shortest.paths(g, 5, 1)
the answer is:
[[1]]
[1] 5 4 3 2
I want the node names not their numbers. Is there any solution? I checked vpath, too.
This does the trick for me:
paths <- get.shortest.paths(g, 5, 1)$vpath
names <- V(g)$name
lapply(paths, function(x) { names[x] })
There is a slightly simpler solution that does not use lapply:
paths <- get.shortest.paths(g, 5, 1)
V(g)$name[paths$vpath[[1]]]

Sort a table based on one condition and the list remaining rows accordingly

I had a table with following details
cID sID Name pID childrenCount
1 1 Site 1 5
2 1 Safty 2 4
3 1 Archit 3 3
4 1 Civil 1 0
5 1 Concs 1 0
6 1 Pavm 1 0
7 1 Paint 3 0
8 1 Alum 3 0
9 1 Doors 3 0
10 1 Highw 1 0
11 1 Road 1 0
12 1 Alarm 2 0
13 1 Safty 2 0
14 1 Fence 2 0
15 1 Beaco 2 0
What I want is to write a select query to order the above table first by their childrenCount values in descending order and the list the corresponding rows according as below
cID sID Name pid childrenCount
1 1 Site 1 5
4 1 Civil 1 0
5 1 Conc 1 0
6 1 Pavm 1 0
10 1 Highw 1 0
11 1 Road 1 0
2 1 Safty 2 4
12 1 Alarm 2 0
13 1 Safty 2 0
14 1 Fence 2 0
15 1 Beacon 2 0
3 1 A WRK 3 3
7 1 Paint 3 0
8 1 Alumin 3 0
9 1 Doors 3 0
Thanks In Advance
Try this...first order by pid then childrenCount desc.
SELECT * FROM TABLENAME order by pid, childrenCount desc
Try this:
SELECT * FROM tableA ORDER BY pid, childrenCount DESC, cid