php - mysql - querying 2 tables in one query and creating the difference of the two results. -


i'm running 2 queries in php, take result of each create difference:

$sec = 3600;     $sql = "select sum(revenue) c revenue_log entry_date between (date_sub(now(), interval $secs second)) , (now())"; $res = $this->db->query($sql)->result_array(); $rev =  $res[0]['c'];  $sql = "select sum(cost) c cost_log entry_date between (date_sub(now(), interval $secs second)) , (now())"; $res = $this->db->query($sql)->result_array(); $cost =  $res[0]['c']; $profit = $rev - $cost; 

is possible combine 1 query easily? how?

you need run each query individually join them derived tables:

select r,c, r-c difference (      select sum(revenue) r      revenue_log       entry_date between (date_sub(now(), interval $secs second)) , (now())      ) revenue join (      select sum(cost) c       cost_log       entry_date between (date_sub(now(), interval $secs second)) , (now())      ) cost; 

Comments

Popular posts from this blog

c# - Unity IoC Lifetime per HttpRequest for UserStore -

Change the color of an oval at click in Java AWT -

I am trying to solve the error message 'incompatible ranks 0 and 1 in assignment' in a fortran 95 program. -