MySQL where left join is the highest -


i want select sum of piece specific case sharing same job. want result highest job.

i have following table (3dprint)

+------+---------+-----------+--------+ | p_id | case_id | qty_piece | job_id | +------+---------+-----------+--------+ |    1 |     186 |         1 |      5 | |    2 |     186 |         2 |      5 | |    3 |     186 |         4 |      7 | +------+---------+-----------+--------+ 

so tried

select sum(qty_piece) 3dprint 3dprint_caseid = 186  order 3dprint_jobid desc limit 1 

but give me 7. want 4. how proceed working ?

while @p. jairaj 's answer correct, there's no need grouping if send case_id. use simpler query follows.

select qty_piece 3dprint case_id = 186  , job_id in (select max(job_id) 3dprint case_id = 186) 

and using query small typo fixes , removing sum function irrelevant:

select qty_piece 3dprint case_id = 186  order job_id desc limit 1 

you're ordering job_id , limiting 1, no need sum function. use sum function when want add values (totaling)


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -