I have a table structure like this:
Stock Table
id  stock
1   0
2   113
3   34
4   50
Products Table (Both tables are connected with ID column)
id   parid   name
1    1       A
2    1       B
3    2       C
4    3       D
I'm using Group by parid in my query which is a requirement of my project.
Query:
select * 
from products as p inner join 
     stock as s on p.id=s.id 
group by parid 
order by stock DESC
It displays result as:
id  parid   name
4   3       D
3   2       C
1   1       A
What I want is:
Group by should include the products according to the stock of product in descending order.
So query should fetch below result rather than the above one:
id  parid  name
4   3      D
3   2      C
2   1      B
Any help will be appreciated.
This may be an answer
Using the below two answers, I came up with the below query which presently seems to solve the problem (still need to check with multiple cases).
SELECT * FROM products p inner join stock i
on p.id=i.id inner join
(select max(stock) maxstock,parid from products inner join stock on products.id=stock.id group by parid) q
on q.parid=p.parid and q.maxstock=i.stock
group by p.parid
 
     
    