mysql - Getting the sum of several columns from two tables result is not correct -


i'am trying sum of 2 columns different tables, have found great posts on stack. of them helped me out. still can't solve problem out..

this query somehow down below returns incorrect total of sum of coulmns, ( rate coulmn - materialprice column )

mysql> tbl  as_servicetickets; +----------+----------+ |ticket_id |  rate    | +----------+----------+ |       11 |   250.00 | |       11 |   300.00 | |       11 |   400.00 | |        9 |   300.00 | |        9 |   300.00 | |        9 |  1500.00 | |        9 |   250.00 | +----------+----------+ total 2 350.00  mysql> tbl as_ticketmaterials; +----------+---------------+ |ticket_id | materialprice | +----------+---------------+ |       11 |           100 | |        9 |            20 | |        9 |            50 | +----------+---------------+ total 70.00 

query---------------------////

select sum(`as_servicetickets`.`rate`) `sercnt`,  sum(`as_ticketmaterials`.`materialprice`) `sercnt`  `as_servicetickets`, `as_ticketmaterials` `as_servicetickets`.`ticket_id` = 9 ,   `as_ticketmaterials`.`ticket_id` = 9 group `as_servicetickets`.`ticket_id`, `as_ticketmaterials`.`ticket_id  result ------------------///// not correct +---------+--------+ | sercnt  | sercnt | +---------+--------+ | 4700.00 |    280 | +---------+--------+ 

this not correct way achieve desired result. try rather:-

select (select sum(`as_servicetickets`.`rate`) `sercnt`         `as_servicetickets`         `as_servicetickets`.`ticket_id` = 9),        (select  sum(`as_ticketmaterials`.`materialprice`) `sercnt`          `as_ticketmaterials`         `as_ticketmaterials`.`ticket_id` = 9); 

Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

python - NameError: name 'subprocess' is not defined -