mysql 对除具有特定标志的记录之外的所有记录进行排序
mysql sort all records except for the one that have specific flag
这是我的查询
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
这个查询的输出是
我想要实现的是显示所有在顶部有 orders_status=1 的记录,然后按 [ 排序剩余的记录=30=](目前按此字段排序)
我需要按 员工姓名 排序,但 orders_status = 1 的除外(将在顶部)
谢谢
我认为你必须加入工会,因为 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 然后按名称
订购
只需在employee_name
前加上order_status = 1 desc,
:
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
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
这是我的查询
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
这个查询的输出是
我想要实现的是显示所有在顶部有 orders_status=1 的记录,然后按 [ 排序剩余的记录=30=](目前按此字段排序)
我需要按 员工姓名 排序,但 orders_status = 1 的除外(将在顶部)
谢谢
我认为你必须加入工会,因为 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 然后按名称
订购只需在employee_name
前加上order_status = 1 desc,
:
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
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