How to get total of parents categories? - mysql

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.

Related

Sum of all child and assign to parent in hierarchical data in MySql

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

Want to display only child if it exists or else only parent table data

I just want to replace the reference id with name from item table in the result as I showed in last table
1st table and Parent table
id
item
amount
tax
status
1
4
20
2
Y
2
5
15
1
N
3
6
5
0
N
2nd table and child table
id
item
p_id
amount
tax
1
1
1
10
1
2
2
2
10
1
3
3
1
15
1
3rd table
id
item
1
mobile
2
heater
3
mouse
4
electronic
5
software
6
papers
What I get
item
amount
tax
1
10
1
2
10
1
3
15
1
5
15
1
6
5
0
What I want is
item
amount
tax
mobile
10
1
heater
10
1
mouse
15
1
software
15
1
papers
5
0
iam using this code it only works with two tables but i want to use it with three tables
SELECT IFNULL(child.item, parent.item) AS item,
IFNULL(child.amount, parent.amount) AS amount,
IFNULL(child.tax, parent.tax) AS tax
FROM parent
LEFT JOIN child ON parent.id = child.p_id

Concat results by days ranges

I have an apartment table:
Id: name:
1 apartment1
2 apartment2
3 apartment3
3 apartment4
And an availabilities table:
Id apartment_id availability_date remaning_number_places
1 1 01/02/2017 3
2 1 02/02/2017 2
3 1 03/02/2017 2
4 1 04/02/2017 2
5 2 01/02/2017 2
6 2 03/02/2017 2
7 2 04/02/2017 2
8 3 12/02/2017 2
9 3 03/02/2017 1
10 3 04/02/2017 1
11 4 12/02/2017 2
12 4 02/02/2017 2
I would like to do a search by starting and ending date with a minimum number of places, for example if I search from 01/02/2017 to 04/02/2017 I should have this result:
Apartment_ids availabilities starting_date ending_date nbr_places
[1] [1,2,3,4] 01/02/2017 04/02/2017 2
[2,4] [5,6,7,12] 01/02/2017 04/02/2017 2
The result must be a concatenation of contiguous days corresponding to given dates.
Thank you at advance.
EDIT : Trying to give more details hereunder:
I need an SQL query to look for an apartment availability for a given period.
As a result, if an apartment is available for the whole period I'm expecting to get it. In addition, I also need to get if more than one apartment are required to cover this given period. In that case, I'm expecting a contiguous list of apartments as a result.
Please, can someone help me build this query?

Most recent distinct record from a joined MySQL table

I have two tables, one of a list of competition results, and one with ELO ratings (based on previous competitions).
Fetching the list of competitors for an arbitrary competition is trivial enough, but I also need to get the most recent rating value for them.
score:
id | eventid | competitorid | position
1 1 1 1
2 1 2 2
3 1 3 3
4 2 2 1
5 2 3 2
6 3 1 1
7 3 3 2
8 3 2 3
rating:
id | competitorid | rating
1 1 1600
2 2 1500
3 3 1500
4 2 1600
5 3 1590
Expected output for a query against score.eventid = 3 would be
id | competitorid | position | rating
6 1 1 1600
7 3 2 1590
8 2 3 1600
At the moment my code looks like:
SELECT score.scoreID, score.competitorID, score.position,
rating.id, rating.rating
FROM score, rating
WHERE score.competitorid = rating.competitorid
AND score.eventid = 3
ORDER BY score.position
which gives an output of
id | competitorid | position | rating.id | rating
6 1 1 1 1600
7 3 2 2 1500
7 3 2 4 1590
8 2 3 3 1500
8 2 3 5 1600
basically it's showing the data from the score table for that correct event, but giving me a row for every rating available against that competitorID unfortunately I have no idea where to build in the DISTINCT statement or how to limit it to the most recent result.
MySQL noob, and managed DISTINCT statements, but not with joins. Unfortunately most previous questions seemed to deal with getting distinct results from a single table, which is not quite what I'm after. Thanks!
One way to get the rating is with a correlated subquery:
SELECT s.scoreID, s.eventID, s.competitorID, s.position,
(select r.rating
from rating r
where s.competitorID = r.competitorID
order by r.id desc
limit 1
) as rating
FROM score s
WHERE s.eventID = 3
ORDER BY s.position;
I'm not sure what ratingprid is, so this only includes the rating.

I need sql query for getting the Required format

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/