Sum Two Columns in Two Mysql Tables

后端 未结 2 2090
天命终不由人
天命终不由人 2020-12-07 01:16

I\'ve been searching everywhere for this but no cigar. Smoke is starting to come out of my ears. please help

How do you sum two columns in two tables and group by us

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-07 01:18

    If you're selecting from 2 tables you need to join them. Otherwise MySQL will not know how to link up the two tables.

    select sum(recipe_num_views + meal_num_views) 
    from recipe r
    inner join meals m ON (r.user_id = m.user_id)
    group by m.user_id
    

    See:
    http://dev.mysql.com/doc/refman/5.5/en/join.html
    http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html

提交回复
热议问题