mysql - Join two table with multiple references and two values to add -


i have following tables:

table article id     v1 ========= 0      10 1      7 2      0 3      0 4      5 5      0  table article_details id     articleid    v2 ====================== 0      0            2 1      4            7 2      4            3 3      5            1 4      1            2 5      4            2 

as can see, table "article" contains entry have column "v1" stands "value1".

the table "article_details" contains reference article , 2nd value ("v2"). however, reference article can appear multiple times in "article_details".

the query looking results in table first column articleid (which can appear once in result). 2nd column needs contain "v1" + of "v2"s can found in "article_details" reference articleid.

from sample data above, need query join 2 tables have following result (v3 = v1 + v2 (+v2') + (v2'')):

table joined articleid     v3 ================ 0             12 1             9 2             0 3             0 4             17 5             1 

v3 article id=4 calculated: v3 = 5 + 7 + 3 + 2 = 17.

what query like?

to can use union inside subquery. union stack results both tables on top of each other, outer query sum values each id:

select id, sum(value) value     (         select id, v1 value article         union         select articleid, v2 article_details     )articleunion group id 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -