使用IF过滤数据

时间:2014-09-27 07:23:20

标签: mysql if-statement

students(studentID PK, A, B,sec..) FeesStructure(feeStruct PK, feeA, feeB, ..) FeesCollected(FeesID PK,studentID FK,feeStruct FK, A,B,..)

如果students.sec ='A'返回FeesStructure.feeA,FeesCollected.A ..等 必须创建一个基于if返回数据的视图。费用结构表有费用A,费用B. 仅根据学生的秒数申请。我需要根据students.sec

返回feeA或feeB

1 个答案:

答案 0 :(得分:0)

您可以使用CASE

select students.studentID,
  case students.sec
    when 'A' then FeesStructure.feeA
    else FeesStructure.feeB
  end as feesStructure,
  case students.sec
    when 'A' then FeesCollected.A
    else FeesCollected.B
  end as feesCollected
from students, FeesStructure, FeesCollected
where students.studentID=FeesStructure.studentID
  and students.studentID=FeesCollected.studentID