如何基于mysql中的多个表计算一列

时间:2015-04-28 20:28:18

标签: mysql

tables

sql import file

--sources (id, name)---
1, "source one"
2, "source two"
3, "source three"


--first_related_items (value, source_id) ---
0, 1    

--second_related_items (value, source_id) ---
0, 2
2, 3

--third_related_items (value, source_id) ---
1, 3

我在多个其他表中有源行和相关项。如何根据其他表获取计算列?计算列将用于源表中的每一行。喜欢

 SELECT 
   sources.name as name, 
   COMPUTED_VALUE(
     first_related_items, 
     second_related_items, 
     third_related_items) as result 
 FROM sources

计算列的派生类似

if(value for source row in first table == 0){
     return 0
}
else if(value for source row in second table == 0){
  if(value for source row in third table == 1){
    return 1
  } else {
    return 0
  }
} else if(value for source row in third table != NULL){
    return value for source row in third table
} else {
   return -1
}

1 个答案:

答案 0 :(得分:0)

您可以加入所有四个表并将此列计算为sqlCom.ExecuteScalar()表达式:

case