mysql对除具有特定标志的记录之外的所有记录进行排序

时间:2017-04-19 06:39:04

标签: mysql sql group-by sql-order-by

这是我的查询

select o.orders_id, o.employee_name,o.payment_method,
o.currency_value, o.order_ref_number,o.orders_status,o.remaining_qty, 
s.orders_status_name, ot.value as order_total,
sum( op.products_quantity ) as num_pieces from orders o 
left join orders_total ot on (o.orders_id = ot.orders_id) 
left join orders_products op on ( o.orders_id = op.orders_id ) 
left join customers c on c.customers_id = o.customers_id 
left join customers_groups cg using(customers_group_id), orders_status s 
where o.orders_status = s.orders_status_id and s.language_id = '1' 
and s.orders_status_id !=17 and ot.class = 'ot_total'
group by o.orders_id 
order by o.employee_name, o.target_ship_date, o.customers_name

此查询的输出是 enter image description here

我想要实现的是显示所有 orders_status = 1的记录,然后按 employee_name 订购剩余记录(目前按照这个领域)

我需要按员工姓名排序,除了那些有order_status = 1(将在顶部)的人

感谢

3 个答案:

答案 0 :(得分:0)

我认为你必须使用联盟,因为orders_status超过2。

select [...]
from [multiple joins]
where orders_status = 1
order by empolyee name

UNION

select [...]
from [multiple joins]
where orders_status <> 1
order by employee name

如果只有2个状态(假设为1表示成功,0表示失败),您可以按order_status订购,然后命名

答案 1 :(得分:0)

只需在order_status = 1 desc,之前添加employee_name

SELECT o.orders_id, 
       o.employee_name, 
       o.payment_method, 
       o.currency_value, 
       o.order_ref_number, 
       o.orders_status, 
       o.remaining_qty, 
       s.orders_status_name, 
       ot.value                  AS order_total, 
       Sum(op.products_quantity) AS num_pieces 
FROM   orders o 
       LEFT JOIN orders_total ot 
              ON ( o.orders_id = ot.orders_id ) 
       LEFT JOIN orders_products op 
              ON ( o.orders_id = op.orders_id ) 
       LEFT JOIN customers c 
              ON c.customers_id = o.customers_id 
       LEFT JOIN customers_groups cg USING(customers_group_id), 
       orders_status s 
WHERE  o.orders_status = s.orders_status_id 
       AND s.language_id = '1' 
       AND s.orders_status_id != 17 
       AND ot.class = 'ot_total' 
GROUP  BY o.orders_id 
ORDER  BY ( o.order_status = 1 ) DESC, 
          o.employee_name, 
          o.target_ship_date, 
          o.customers_name 

<强> 编辑:

SELECT o.orders_id, 
       o.employee_name, 
       o.payment_method, 
       o.currency_value, 
       o.order_ref_number, 
       o.orders_status, 
       o.remaining_qty, 
       s.orders_status_name, 
       ot.value                  AS order_total, 
       Sum(op.products_quantity) AS num_pieces 
FROM   orders o 
       LEFT JOIN orders_total ot 
              ON ( o.orders_id = ot.orders_id ) 
       LEFT JOIN orders_products op 
              ON ( o.orders_id = op.orders_id ) 
       LEFT JOIN customers c 
              ON c.customers_id = o.customers_id 
       LEFT JOIN customers_groups cg USING(customers_group_id), 
       orders_status s 
WHERE  o.orders_status = s.orders_status_id 
       AND s.language_id = '1' 
       AND s.orders_status_id != 17 
       AND ot.class = 'ot_total' 
GROUP  BY o.orders_id 
ORDER  BY ( o.order_status = 1 ) DESC, 
          o.employee_name + 0, 
          o.target_ship_date, 
          o.customers_name 

答案 2 :(得分:0)

UPDATE1,如果您不想在orders_status = 1时使用员工姓名:

select 
    o.orders_id, 
    o.employee_name,
    o.payment_method,
    o.currency_value, 
    o.order_ref_number,
    o.orders_status,
    o.remaining_qty, 
    s.orders_status_name, 
    ot.value as order_total,
    sum( op.products_quantity ) as num_pieces 
from orders o 
    left join orders_total ot on (o.orders_id = ot.orders_id) 
    left join orders_products op on ( o.orders_id = op.orders_id ) 
    left join customers c on c.customers_id = o.customers_id 
    left join customers_groups cg using(customers_group_id), orders_status s 
where 
    o.orders_status = s.orders_status_id 
    and s.language_id = '1' 
    and s.orders_status_id !=17 
    and ot.class = 'ot_total'
group by o.orders_id 
order by 
    case
        when o.orders_status = 1 then 0
        else 1
    end asc,
    case
        when o.orders_status <> 1 then o.employee_name
    end asc,
    o.target_ship_date, o.customers_name