1. 程式人生 > >mysql case when記錄

mysql case when記錄

小記

當type=8時,startname從users表獲取,當type=13時,startname從warehouse表獲取
$sql = 'select id,start, type, (case
                   when type = ? THEN
                    (select truename
                       from users
                      where users.id = start  limit 1)
                   when type = ? then
                    (select name
                       from warehouse wh
                      where wh.id = start limit 1)
                 end) AS startname, (
                   select name
                       from warehouse wh
                      where wh.id = destination
                  ) AS endname
                 FROM terminal_operation where type in (?,?)';
$res = DB::select($sql, [8,13,8,13]);