使用mysql以三个条件排序

时间:2014-12-19 07:55:51

标签: mysql

我有一个连接查询,我需要记录三个条件的顺序   1.那些记录purch_order_workflow_code_nr ='3'先来   2.那些记录状态=空来的第二名   3.之后所有扩孔记录

请给我一个合适的解决方案我该怎么写呢 我试过这个代码。在我的查询中,但我没有得到我想要的

SELECT purch_order.user_code_tx as user_code_tx ,purch_order.date_creation 
    as date_creation ,purch_order.purch_order_code_nr as purch_order_code_nr 
    ,purch_order.status as status ,purch_order.purch_order_workflow_code_nr as purch_order_workflow_code_nr ,ppod.vehicle_plate_tx as vehicle_plate_tx ,(Select pd.purch_order_detail_desc_tx from purch_order_detail pd where pd.purch_order_code_nr = purch_order.purch_order_code_nr limit 1 ) as purch_order_detail_desc_tx ,(Select pd.purch_order_workflow_desc_tx from purch_order_workflow pd where pd.purch_order_workflow_code_nr = purch_order. purch_order_workflow_code_nr limit 1 ) as purch_order_workflow_desc_tx ,vehicle.op_base_code_nr as op_base_code_nr ,company_division.div_name_tx as div_name_tx ,company_division_op_bases.op_base_name as op_base_name 
FROM purch_order inner join (Select distinct pod.purch_order_code_nr, pod.vehicle_plate_tx from purch_order_detail pod) ppod on ppod.purch_order_code_nr = purch_order.purch_order_code_nr inner join vehicle on vehicle.vehicle_plate_tx = ppod.vehicle_plate_tx inner join company_division on company_division.div_code_nr= purch_order.div_code_nr inner join company_division_op_bases on company_division_op_bases.op_base_code_nr = vehicle.op_base_code_nr 
WHERE purch_order.company_code_nr='1000' LIMIT 7; 

3 个答案:

答案 0 :(得分:0)

这应该适合你:

SELECT purch_order.user_code_tx as user_code_tx ,purch_order.date_creation 
    as date_creation ,purch_order.purch_order_code_nr as purch_order_code_nr ,purch_order.status as status ,purch_order.purch_order_workflow_code_nr as purch_order_workflow_code_nr ,ppod.vehicle_plate_tx as vehicle_plate_tx ,(Select pd.purch_order_detail_desc_tx from purch_order_detail pd where pd.purch_order_code_nr = purch_order.purch_order_code_nr limit 1 ) as purch_order_detail_desc_tx ,(Select pd.purch_order_workflow_desc_tx from purch_order_workflow pd where pd.purch_order_workflow_code_nr = purch_order. purch_order_workflow_code_nr limit 1 ) as purch_order_workflow_desc_tx ,vehicle.op_base_code_nr as op_base_code_nr ,company_division.div_name_tx as div_name_tx ,company_division_op_bases.op_base_name as op_base_name, 
    CASE 
    WHEN purch_order_workflow_code_nr = '3' THEN 1
    WHEN status = '' THEN 2
    ELSE 3
    END
    AS order_bit
FROM purch_order inner join (Select distinct pod.purch_order_code_nr, pod.vehicle_plate_tx from purch_order_detail pod) ppod on ppod.purch_order_code_nr = purch_order.purch_order_code_nr inner join vehicle on vehicle.vehicle_plate_tx = ppod.vehicle_plate_tx inner join company_division on company_division.div_code_nr= purch_order.div_code_nr inner join company_division_op_bases on company_division_op_bases.op_base_code_nr = vehicle.op_base_code_nr 
WHERE purch_order.company_code_nr='1000' 
ORDER BY order_bit
LIMIT 7; 

答案 1 :(得分:0)

首先。您必须格式化查询,您可以更好地理解。 第二。格式化此查询后,您可以查看订单的位置

SELECT purch_order.user_code_tx as user_code_tx ,
	   purch_order.date_creation as date_creation ,
	   purch_order.purch_order_code_nr as purch_order_code_nr ,
	   purch_order.status as status ,
	   purch_order.purch_order_workflow_code_nr as purch_order_workflow_code_nr ,
	   ppod.vehicle_plate_tx as vehicle_plate_tx ,
	   (Select pd.purch_order_detail_desc_tx 
		  from purch_order_detail pd 
		 where pd.purch_order_code_nr = purch_order.purch_order_code_nr 
		 limit 1 ) as purch_order_detail_desc_tx ,
	   (Select pd.purch_order_workflow_desc_tx 
	      from purch_order_workflow pd 
		 where pd.purch_order_workflow_code_nr = purch_order. purch_order_workflow_code_nr 
		 limit 1 ) as purch_order_workflow_desc_tx ,
	   vehicle.op_base_code_nr as op_base_code_nr ,
	   company_division.div_name_tx as div_name_tx ,
	   company_division_op_bases.op_base_name as op_base_name 
FROM purch_order 
inner join (Select distinct pod.purch_order_code_nr, pod.vehicle_plate_tx from purch_order_detail pod) ppod 
on ppod.purch_order_code_nr = purch_order.purch_order_code_nr 
inner join vehicle 
on vehicle.vehicle_plate_tx = ppod.vehicle_plate_tx 
inner join company_division 
on company_division.div_code_nr= purch_order.div_code_nr 
inner join company_division_op_bases 
on company_division_op_bases.op_base_code_nr = vehicle.op_base_code_nr 
WHERE purch_order.company_code_nr='1000'
-- ORDER LOCATION --
LIMIT 7
和第三。一个简单的示例顺序,字段1,字段2,字段3.

SELECT purch_order.user_code_tx as user_code_tx ,
	   purch_order.date_creation as date_creation ,
	   purch_order.purch_order_code_nr as purch_order_code_nr ,
	   purch_order.status as status ,
	   purch_order.purch_order_workflow_code_nr as purch_order_workflow_code_nr ,
	   ppod.vehicle_plate_tx as vehicle_plate_tx ,
	   (Select pd.purch_order_detail_desc_tx 
		  from purch_order_detail pd 
		 where pd.purch_order_code_nr = purch_order.purch_order_code_nr 
		 limit 1 ) as purch_order_detail_desc_tx ,
	   (Select pd.purch_order_workflow_desc_tx 
	      from purch_order_workflow pd 
		 where pd.purch_order_workflow_code_nr = purch_order. purch_order_workflow_code_nr 
		 limit 1 ) as purch_order_workflow_desc_tx ,
	   vehicle.op_base_code_nr as op_base_code_nr ,
	   company_division.div_name_tx as div_name_tx ,
	   company_division_op_bases.op_base_name as op_base_name 
FROM purch_order 
inner join (Select distinct pod.purch_order_code_nr, pod.vehicle_plate_tx from purch_order_detail pod) ppod 
on ppod.purch_order_code_nr = purch_order.purch_order_code_nr 
inner join vehicle 
on vehicle.vehicle_plate_tx = ppod.vehicle_plate_tx 
inner join company_division 
on company_division.div_code_nr= purch_order.div_code_nr 
inner join company_division_op_bases 
on company_division_op_bases.op_base_code_nr = vehicle.op_base_code_nr 
WHERE purch_order.company_code_nr='1000'
ORDER BY user_code_tx , date_creation,purch_order_code_nr
LIMIT 7

答案 2 :(得分:0)

在你的sql中添加额外的flg会有所帮助。 例如:

SELECT 
    ..., 
    IF(purch_order_workflow_code_nr = '3', 1, 2) as powcn_flg,
    IF(status='empty', 1, 2) as statsu_flg
FROM ...
WHERE ...
ORDER BY powcn_flg ASC, status_flg ASC

所以如果你把它改成这个,你的sql可能会有用:

SELECT 
purch_order.user_code_tx as user_code_tx ,
purch_order.date_creation as date_creation ,
purch_order.purch_order_code_nr as purch_order_code_nr ,
purch_order.status as status ,purch_order.purch_order_workflow_code_nr as urch_order_workflow_code_nr ,
ppod.vehicle_plate_tx as vehicle_plate_tx ,
(Select pd.purch_order_detail_desc_tx from purch_order_detail pd where pd.purch_order_code_nr = purch_order.purch_order_code_nr limit 1 ) as purch_order_detail_desc_tx 
,(Select pd.purch_order_workflow_desc_tx from purch_order_workflow pd where pd.purch_order_workflow_code_nr = purch_order. purch_order_workflow_code_nr limit 1 ) as purch_order_workflow_desc_tx ,
vehicle.op_base_code_nr as op_base_code_nr ,
company_division.div_name_tx as div_name_tx ,
company_division_op_bases.op_base_name as op_base_name ,

IF(purch_order_workflow_code_nr = '3', 1, 2) as powcn_flg,
IF(status='empty', 1, 2) as statsu_flg

FROM purch_order 
inner join (Select distinct pod.purch_order_code_nr, pod.vehicle_plate_tx from purch_order_detail pod) ppod on ppod.purch_order_code_nr = purch_order.purch_order_code_nr 
inner join vehicle on vehicle.vehicle_plate_tx = ppod.vehicle_plate_tx 
inner join company_division on company_division.div_code_nr= purch_order.div_code_nr 
inner join company_division_op_bases on company_division_op_bases.op_base_code_nr = vehicle.op_base_code_nr 
WHERE purch_order.company_code_nr='1000' 
ORDER BY powcn_flg ASC, status_flg ASC
LIMIT 7;