Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sqlite 从一列中选择语句顺序(按top 3排序),其余的按另一列排序_Sqlite - Fatal编程技术网

Sqlite 从一列中选择语句顺序(按top 3排序),其余的按另一列排序

Sqlite 从一列中选择语句顺序(按top 3排序),其余的按另一列排序,sqlite,Sqlite,我需要使用两个不同的排序器。我想根据列PopularitySortOrder ASC选择前3名,其余按SortOrder ASC选择 表格 --------------------------------------------------------------------------------------- | Id | Product | Price | SortOrder | PopularitySortOrder | ----

我需要使用两个不同的排序器。我想根据列PopularitySortOrder ASC选择前3名,其余按SortOrder ASC选择

表格

---------------------------------------------------------------------------------------
|   Id   |      Product         |   Price   |   SortOrder   |   PopularitySortOrder   |
---------------------------------------------------------------------------------------
    1     Samsung Galaxy S6 Edge     100            1                   2 
    2          iPhone 6              100            2                   1 
    3          iPhone 5S             100            4                   4 
    4       Samsung Galaxy S6        100            6                   3 
    5       Google Nexus 6           100            3                   5 
    6       Google Nexus 5           100            5                   7 
我尝试了以下选择,但始终没有成功:

select *
FROM
(
   select * from Temp1 t1 order by PopularitySortOrder LIMIT 3
   union all
   select * from Temp1 t2 where t2.Id <> t1.Id order by SortOrder
) 
order by PopularitySortOrder, SortOrder asc
---------------------------------------------------------------------------------------
|   Id   |      Product         |   Price   |   SortOrder   |   PopularitySortOrder   |
---------------------------------------------------------------------------------------
    2          iPhone 6              100            2                   1 *
    1     Samsung Galaxy S6 Edge     100            1                   2 *
    4       Samsung Galaxy S6        100            6                   3 *
    5       Google Nexus 6           100            3 *                 5 
    3          iPhone 5S             100            4 *                 4 
    6       Google Nexus 5           100            5 *                 7 
编辑:使用SQLite版本3.7


t1
在联合查询的其他部分不可见

您可以复制流行度查询,或使用临时视图,或(在SQLite 3.8.3或更高版本中)使用:

WITH MostPopular3 AS (
  SELECT * FROM Temp
  ORDER BY PopularitySortOrder
  LIMIT 3
)
SELECT * FROM MostPopular3
UNION ALL
SELECT * FROM (
  SELECT * FROM Temp
  WHERE Id NOT IN (SELECT Id FROM MostPopular3)
  ORDER BY SortOrder
)