Mysql 如何在联合查询中按下单
我需要在联合内部使用order byMysql 如何在联合查询中按下单,mysql,Mysql,我需要在联合内部使用order by 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_i
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')
有人能帮我吗。一种方法:
SELECT *
FROM (select blp_res_id as id,blp_res_file_name....)
ORDER BY some_column
仅在第一个选择中使用别名,并将order by添加到最后一个选择中,例如:ordering by name
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
试试这个。您可以不使用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>
太多太多太多太多太多太多太多太多太多太多太多