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]);

posted on 2019-01-10 11:49  拙言者  阅读(111)  评论(0编辑  收藏  举报

导航