SQL OR逻辑排序

时间:2018-01-26 07:50:07

标签: php sql sql-server

我有游戏专栏PC=1/0XBX=1/0PS4=1/0。我将根据为PCXBXPS4选中的复选框返回结果并获取结果。

如果使用pc=1 OR xbx=1 OR ps4=1检查平台。

我正在使用像这样的PHP内爆。

MySQL query for multiple checkbox values

PHP内爆完美无缺,但如果全部是plt。

查询

SELECT  p1.date_id as rDate,     
        p1.game_name as gameName,         
        p1.release_type as rType,     
        p1.notes as rNotes,     
        on_pc as is_PC,     
        on_xb1 as is_XB1,     
        on_ps4 as is_PS4,     
        ie.[Event Name] as event_name     
FROM dbo.vw_fps_game_list p1     
LEFT JOIN dbo.Industry_Events_db ie ON ie.[Date] = p1.date_id   
WHERE (on_xb1=1 OR on_ps4=1 OR on_xb1)   
ORDER BY on_pc DESC

我需要按此顺序显示结果 如果检查了所有3个平台,  然后where (pc=1 OR xb1=1 OR ps4=1)需要在顶部排序,然后where (pc =1 OR xb1=1 ) / (pc =1 OR ps4=1)/ (ps4 =1 OR xb1=1 ),然后(pc =1) / (xb1=1)/ (ps4=1)

任何帮助都将受到高度赞赏。

2 个答案:

答案 0 :(得分:3)

您可以使用案例设置订单值

  SELECT 
    p1.date_id as rDate
    , p1.game_name as gameName
    , p1.release_type as rType
    , p1.notes as rNotes
    , on_pc as is_PC
    , on_xb1 as is_XB1
    , on_ps4 as is_PS4
    , ie.[Event Name] as event_name     
    , case when on_pc = 1 and on_xb1 = 1 and on_ps4 = 1 then 0
           when on_pc_= 1 and on_xb1 = 1 and on_ps4 = 0 then 1
           when on_pc_= 1 and on_xb1 = 0 and on_ps4 = 0 then 2
               ELSE 3
       END as my_order
  FROM dbo.vw_fps_game_list p1     
  LEFT JOIN dbo.Industry_Events_db ie on ie.[Date] = p1.date_id   
  WHERE (on_xb1=1 or on_ps4=1 or on_xb1)   
  ORDER BY my_order

答案 1 :(得分:1)

根据您的上一段,您可以使用以下脚本实现您的目标。

   SELECT p1.date_id       as rDate
        , p1.game_name     as gameName
        , p1.release_type  as rType
        , p1.notes         as rNotes
        , on_pc            as is_PC
        , on_xb1           as is_XB1
        , on_ps4           as is_PS4
        , ie.[Event Name]  as event_name
     FROM dbo.vw_fps_game_list p1
LEFT JOIN dbo.Industry_Events_db ie
       ON ie.[Date] = p1.date_id
    WHERE (on_xb1=1 or on_ps4=1 or on_xb1)
 ORDER BY CASE WHEN      on_pc  = 1
                     AND on_ps4 = 1
                     AND on_xb1 = 1                     THEN 1
               WHEN     (on_pc  = 1 AND on_xb1 = 1)
                     OR (on_pc  = 1 AND on_ps4 = 1)
                     OR (on_ps4 = 1 AND on_xb1 = 1)     THEN 2
               WHEN     (on_pc  = 1)
                     OR (on_xb1 = 1)
                     OR (on_ps4 = 1)                    THEN 3
               ELSE 4
           END ASC