0

I have two tables

Joined_courses_table

---------------------------------------
id      course1_id   course2_id
---------------------------------------
1           33           3
2           2            1

---------------------------------------

Results_table

---------------------------------------
course_id    value
---------------------------------------
33             50
3              40
2              70
1              130
---------------------------------------

Problem -> I want the Values Sum from results_table for each two pairs in the Joined_courses_table

4

1 回答 1

1

尝试这个:

SELECT
  id, course1_id, course2_id, 
  results1.value + results2.value AS values_sum
FROM
  Joined_courses_table
  JOIN Results_table results1 ON (course1_id = results1.course_id)
  JOIN Results_table results2 ON (course2_id = results2.course_id)

如果您有一个PRIMARY KEYon ,查询应该执行良好Results_table.course_id

于 2012-07-22T14:29:00.353 回答