i am having table Category with the columns like
id(AutoIncrement),Parent_id,Level,Name
initially for level 1 datas has Parent_id is 0. autoincrement id will be Parent_id for next levels .my table table table datas will bw like this
id Parent_id Level Name
1 0 1 Indian
2 0 1 International
3 0 1 Nri
4 1 2 BC
5 2 2 Christian
6 2 2 Muslim
7 4 3 MBC-1
8 7 4 OBC-2
9 1 2 FC
i want to show records in this format (its like a tree view)
id Parent_id Level Name
1 0 1 Indian
4 1 2 BC
7 4 3 MBC-1
8 7 4 OBC-2
9 1 2 FC
5 2 2 Christian
6 2 2 Muslim
2 0 1 International
3 0 1 Nri
4 1 2 BC
Can any one should help me to get this arrangement of datas using sql Query?
If it does not have a set number of branches you likely want to loop over a query in your app or write an SP to obtain all nodes. Some good reading here:
http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/
Related
I have a target where I have this DUPLICATE checker with COUNT for multiple arrays. As we can see. Any suggestions on how I make this work? Thank you and have a nice day.
DATA:
id
value
1
[5,6,8,4,2]
2
[2,3,4,1,8]
3
[9,3,2,1,10]
Normal result:
number
count
1
2
2
3
3
2
4
2
5
1
6
1
7
0
8
2
9
1
10
1
This is my target result with sorting (Highest count):
number
count
2
3
1
2
3
2
4
2
8
2
5
1
6
1
10
1
9
1
I have a 2 Mysql tables like 1.Product(This table is hierarchical or recursive) and 2.Sales table in this will store sale id and product id. I need to show parent product with the sum of all hierarchical child product.
Product table having data like,
id name parent_id
1 Necklace NULL
2 Ring NULL
3 Earing NULL
4 Choker 1
5 Long Necklace 1
6 Short Necklace 1
7 2-Line 5
8 3-Line 5
9 Mango 5
10 Green 7
11 Red 7
12 White 7
13 Stud 3
Sales table will have data like,
id product_id no_of_pcs weight rate
1 10 5 40 35000
2 12 8 50 50000
3 9 2 20 25000
4 6 1 8 25000
5 13 2 16 22000
Now I'm trying the result as,
Product sale_pcs tot_pcs sale_wt sale_rate
1 Necklace 16 118 135000
3 Earing 2 16 22000
Now I'm using mysql query inside php recursive function by using product table. By using this even recursive function all the products need to check with sales table. This will happening performance issue. Could you please help any one to solve this issue whether this possible by doing in query itself
I have two tables, the first is the following
"Categories"
id name parent_id
1 Food NULL
2 Vegetable 1
3 Fruit 1
4 Tomatoes 2
5 onion 2
6 bananas 3
7 grapes 3
the second table is the following
"amounts"
id amount category_id
1 100 4
2 50 6
3 25 5
4 100 7
5 50 4
6 25 7
Currently categories table are three levels and I want to return all
the Categories along with total columns:
Means that sum of the category of level 2 is the total of level 3 and
the total of level 2 should was the sum of level 1. I would really
appreciate some help Beforehand thank you very much.
I am trying make a complex query in MySQL i have following two tables
departments employees
Id parent title id name department status
--------------------------- ------------------------------------
1 0 Health 1 abc 3 1
2 0 Sports 2 def 3 1
3 0 Education 3 ghi 5 1
4 1 Physical 4 jkl 10 1
5 1 Mental 5 kkk 6 1
6 2 Football 6 lll 6 1
7 2 Baseball 7 sss 8 1
8 2 Beachball 8 xxx 6 1
9 2 Hockey 9 yyy 6 1
10 4 ENT 10 zzz 7 1
11 0 Finance 11 mnb 11 1
Departments table have four main departments(i-e: parent = 0) with multiple level depths of sub-departments.
Currently i have done it through a PHP function by running queries multiple time to achieve this, but still i want know if this is possible to fetch it with a Query.
Whats the best way OR how to select max 3 employee randomly for each main department with status 1.
The expected result should be something like
id name department maindep
1 abc 3 3
2 def 3 3
3 ghi 5 1
4 jkl 10 1
5 kkk 6 2
7 sss 8 2
10 zzz 7 2
11 mnb 11 11
I keep forgetting -
I am selecting into an output file with terminal MySQL sever operations - I need to create a delimiter between SELECT sets. How can I insert a "---"?
SELECT "---";
Doesn't work obviously.
Also is there a way to format output from MySQL commands?
Mine looks like a mess:
track_id track_title track_num album genre artist length track_rating
1 Siberia 1 1 1 1 00:04:29 5
2 Where the Fence is Low 2 1 1 1 00:03:24 5
3 Toes 3 1 1 1 00:03:19 5
4 Banner 4 1 1 1 00:03:37 5
5 Everybody Breaks a Glass 5 1 1 1 00:03:55 5
6 Heavy Rope 6 1 1 1 00:03:59 5
7 Timing Is Everything 7 1 1 1 00:03:16 5
8 Peace Sign 8 1 1 1 00:03:20 5
9 Flux and Flow 9 1 1 1 00:03:17 5
10 Fourth Dimension 10 1 1 1 00:03:27 5
11 Hollywood 1 2 3 3 00:04:13 5
12 In This Moment 2 2 3 3 00:04:31 5
13 Some Kind of Wonderful 3 2 3 3 00:03:04 5
14 End of May 4 2 3 3 00:03:53 5
15 Me and Mrs. Jones 5 2 3 3 00:03:43 5
16 Have not met you yet 6 2 3 3 00:05:20 5
17 Heartache Tonight 7 2 3 3 00:03:47 5
18 Best of Me 8 2 3 3 00:04:33 5
19 Swimming in Miami 1 4 1 4 00:04:56 5
20 Captains and Cruise Ships 2 4 1 4 00:03:29 5
The command you state "doesn't work, obviously" should work fine in MySQL.
If you feel the need to have a FROM clause in the SQL statement, the docs state that you can use FROM DUAL as "dummy" table.
It's
select '---' ...
Note the single quotes.
I need to create a delimiter between SELECT sets. How can I insert a "---"?
So, you have more than one query and you want the output to have a row with ---. IMO: Awful :)
You'll have to do this:
select field1, field2, field3, etc... from yourTable
union all
select '---', null, null, etc...
union all
select field1, field2, field3, etc... from anotherTable