2017-04-19 44 views
0

這裏的一個MySQL的排序中的所有記錄是我的查詢除非有特定的標誌

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(目前正在此領域的基礎上訂購)

我需要員工姓名排序除了那些有orders_status = 1(這將是在頂部)

感謝

+0

使用 ORDER BY o.orders_status,o.employee_name,o.target _ship_date,o.customers_name 首先將o.orders_status放入ORDER BY子句中。 –

+0

@Muhammad Asif Raza請檢查我的答案,它工作正常嗎? –

回答

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 
+0

感謝您的回答 它訂單上的所有orders_status = 1記錄,但應根據employee_name排序的其餘記錄沒有正確排序.. –

+0

@MuhammadAsifRaza你有沒有跑過這個? – Blank

+0

您的答案不適用於employee_name排序。 Order_status = 1的訂單現在處於頂部,但剩下的(應根據employee_name列排序)沒有正確排序..它顯示所有employee_name,它們是10然後是11然後是12然後是3.哪個是錯誤的 –

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),你可以通過orders_status剛纔排序,然後命名

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 
+0

您的答案不適用於employee_name排序。 Order_status = 1的訂單現在處於頂部,但剩下的(應根據employee_name列排序)沒有訂購..它顯示所有的employee_name,它們是10,然後是11,然後是12,然後是3.這是錯誤的 –

+0

@MuhammadAsifRaza你有什麼數據類型在這個列中?如果它是varchar f.e.,則完全沒有問題。 –

+0

employee_name是整數 –