1. 程式人生 > >union all 排序之後分頁

union all 排序之後分頁

1 . 應用場景

每一個union all 的結果單獨排序, union all 之後進行分頁

2. union all order by 單個語句寫法

SELECT
    * 
FROM
    (
SELECT
    id,
    NAME,
    type,
    create_time AS edit_time,
    data_update AS data_up,
    is_delete 
FROM
    v_dataset 
WHERE
    id IN ( 287, 347, 363 ) 
ORDER BY
    data_up DESC 
    ) AS
b

3. sql

SELECT
    * 
FROM
    (
    (
SELECT
    * 
FROM
    (
SELECT
    f.id,
    f.name,
    f.create_time,
    d.create_time AS edit_time,
    d.data_update AS data_up,
    group_concat( d.id ORDER BY d.update_time DESC ) AS DATA 
FROM
    `v_folder_dataset_relation` `r`
    LEFT JOIN `v_dataset`
`d` ON `r`.`dataset_id` = `d`.`id` JOIN `v_folder` `f` ON `r`.`folder_id` = `f`.`id` WHERE `r`.`is_delete` = 0 AND `r`.`create_by` = 441 AND ( `d`.`is_delete` = 0 OR ( `d`.`is_delete` IS NULL ) ) GROUP BY `r`.`folder_id` ORDER BY create_time DESC ) AS a ) UNION ALL
( SELECT * FROM ( SELECT id, name, type, create_time AS edit_time, data_update AS data_up, is_delete FROM v_dataset WHERE id IN ( 287, 347, 363 ) ORDER BY data_up DESC ) AS b ) ) AS c LIMIT 0, 9