如何在UNION查询中放置顺序

时间:2017-04-25 11:42:57

标签: mysql

我需要在UNION内使用该订单。我正在使用以下查询

SELECT blp_res_id as id,
    blp_res_file_name as name,
    blp_res_publisher as pname,
    blp_res_in_date as startdate,
    blp_res_modified_date as modifieddate,
    TIMEDIFF(NOW(),blp_res_in_date) as timetaken
FROM blp_result_info 
WHERE blp_res_proc_id = '16' and 
    DATE(blp_res_in_date) = DATE('2017-04-25') and 
    blp_res_lock_status = '0' 
UNION 
SELECT blp_res_id as id,
    blp_res_file_name as name,
    blp_res_publisher as pname,
    blp_res_in_date as startdate,
    blp_res_modified_date as modifieddate,
    TIMEDIFF(NOW(),blp_res_in_date) as timetaken 
FROM blp_result_info 
WHERE blp_res_proc_id = '16' and 
    DATE(blp_res_in_date) = DATE('2017-04-25') and 
    blp_res_lock_status = '1' 
UNION 
SELECT blp_res_id as id,
    blp_res_file_name as name,
    blp_res_publisher as pname,
    blp_res_in_date as startdate,
    blp_res_modified_date as modifieddate,
    TIMEDIFF(NOW(),blp_res_in_date) as timetaken 
FROM blp_result_info 
WHERE blp_res_proc_id = (
    select blp_proc_fix_id 
    from blp_process_info 
    where blp_proc_id =16
    ) and 
    DATE( blp_res_in_date) = DATE('2017-04-25')

任何人都可以帮助我。

3 个答案:

答案 0 :(得分:0)

一种方法:

SELECT *
FROM (select blp_res_id as id,blp_res_file_name....)
ORDER BY some_column

答案 1 :(得分:0)

仅在第一个选择中使用别名,并将顺序添加到最后一个选择中,例如:(按名称排序)

select 
   blp_res_id as id
  ,blp_res_file_name as name
  ,blp_res_publisher as pname
  ,blp_res_in_date as startdate
  ,blp_res_modified_date as modifieddate
  ,TIMEDIFF(NOW(),blp_res_in_date) as timetaken 
FROM blp_result_info 
where blp_res_proc_id = '16' 
and DATE(blp_res_in_date) = DATE('2017-04-25') 
and blp_res_lock_status = '0' 

UNION 
select 
     blp_res_id 
    ,blp_res_file_name 
    ,blp_res_publisher 
    ,blp_res_in_date 
    ,blp_res_modified_date 
    ,TIMEDIFF(NOW(),blp_res_in_date) 
FROM blp_result_info 
where blp_res_proc_id = '16' 
and DATE(blp_res_in_date) = DATE('2017-04-25') 
and blp_res_lock_status = '1' 

UNION 
select 
     blp_res_id 
    ,blp_res_file_name 
    ,blp_res_publisher 
    ,blp_res_in_date 
    ,blp_res_modified_date 
    ,TIMEDIFF(NOW(),blp_res_in_date)  
FROM blp_result_info 
where blp_res_proc_id = (
        select blp_proc_fix_id 
        from blp_process_info 
        where blp_proc_id =16) 
and DATE( blp_res_in_date) = DATE('2017-04-25')

ORDER BY name 

答案 2 :(得分:0)

试试这个。您可以在没有UNION的情况下编写查询,因为您在所有3个子查询中查询同一个表。

SELECT blp_res_id as id,
   blp_res_file_name as name,
   blp_res_publisher as pname,
   blp_res_in_date as startdate,
   blp_res_modified_date as modifieddate,
   TIMEDIFF(NOW(),blp_res_in_date) as timetaken
FROM blp_result_info 
WHERE 
(
    blp_res_proc_id = '16' and 
    DATE(blp_res_in_date) = DATE('2017-04-25') and 
    blp_res_lock_status = '0'
)
OR
(
    blp_res_proc_id = '16' and 
    DATE(blp_res_in_date) = DATE('2017-04-25') and 
    blp_res_lock_status = '1'
)
OR
(
    blp_res_proc_id = (
    select blp_proc_fix_id 
    from blp_process_info 
    where blp_proc_id =16
    ) and 
    DATE( blp_res_in_date) = DATE('2017-04-25')
)
ORDER BY <YOUR_COLUMN_NAME>