SELECT stable.scate, stable.stname, s2table.aTime, atable.stname, a2table.aTime, 3table1.available_reservation_seat AS ars, if( stable.scate = 'KTX', 7200 * count( feetable.no ) +8400, if( stable.scate = '새마을', 3136 * count( feetable.no ) +4800, 950 * count( feetable.no ) +2600 ) ) * rtable1.ea as fee

from rtable as rtable1
Inner join 1table as stable
on rtable1.sStion = stable.stname
Inner join 1table as atable
on (rtable1.aStion = atable.stname and stable.scate = atable.scate)

Inner join 2table as s2table
on (stable.no = s2table.t1no and rtable1.Time <= s2table.aTime)
Inner join 2table as a2table
on atable.no = a2table.t1no
Inner join 3table as 3table1
on (rtable1.date < 3table1.dynamic_date + curdate() and 3table1.available_reservation_seat > 0)

right join 2table as feetable
on (feetable.no > s2table.no and feetable.no < a2table.no)
Group by s2table.no