[英]How to sort with multiple conditions in mysql
I have a table like the following with 2 columns product_id, main_product_id and location. 我有一个类似以下的表格,其中有2列product_id,main_product_id和location。 main_product_id contains the product_id of a main product.
main_product_id包含主要产品的product_id。
+-------------+--+--------------+--+-----------+
| product_id | | main_prod_id | | location |
+-------------+--+--------------+--+-----------+
| product1 | | | | AE 34 CH |
| product2 | | | | AE 46 CH |
| product3 | | | | V |
| sub_prod_1 | | product3 | | |
| sub_prod_2 | | product3 | | |
| sub_prod_3 | | product3 | | AK 22 AUS |
| sub_prod_4 | | product3 | | CI 06 FR |
| sub_prod_5 | | product3 | | TA ee ES |
| sub_prod_6 | | product3 | | V |
| product4 | | | | CT 01 FR |
| product5 | | | | V |
| sub_prod_7 | | product5 | | |
| sub_prod_8 | | product5 | | |
| sub_prod_9 | | product5 | | ED 2 ES |
| sub_prod_10 | | product5 | | EN 02 ES |
| sub_prod_11 | | product5 | | TB 03 ES |
| sub_prod_12 | | product5 | | V |
+-------------+--+--------------+--+-----------+
sub_prod_12 product5 V sub_prod_12 product5 V
I want to sort 我想排序
So the final result should look like this 所以最终结果应该像这样
+-------------+--+--------------+-----------+
| Product_id | | main_prod_id | location |
+-------------+--+--------------+-----------+
| product1 | | | AE 34 CH |
| product2 | | | AE 46 CH |
| product4 | | | CT 01 FR |
| product3 | | | V |
| sub_prod_1 | | product3 | |
| sub_prod_2 | | product3 | |
| sub_prod_3 | | product3 | AK 22 AUS |
| sub_prod_4 | | product3 | CI 06 FR |
| sub_prod_5 | | product3 | TA ee ES |
| sub_prod_6 | | product3 | V |
| product5 | | | V |
| sub_prod_7 | | product5 | |
| sub_prod_8 | | product5 | |
| sub_prod_9 | | product5 | ED 2 ES |
| sub_prod_10 | | product5 | EN 02 ES |
| sub_prod_11 | | product5 | TB 03 ES |
| sub_prod_12 | | product5 | V |
+-------------+--+--------------+-----------+
It's possible that the table may have only type 1 products or both type 2 and it's sub products or all types of products. 该表可能仅具有类型1的产品,或者可能既具有类型2的产品,又可能具有子产品或所有类型的产品。
My query so far is the following 我到目前为止的查询如下
SELECT
mt.product_id, mt.main_prod_id, mt.location
FROM
my_table mt
ORDER BY (CASE
WHEN
mt.product_id NOT IN (SELECT
main_prod_id
FROM
my_table)
AND (mt.main_prod_id = ''
OR mt.main_prod_id IS NULL)
THEN
mt.location
WHEN
mt.product_id IN (SELECT
main_prod_id
FROM
my_table)
AND (mt.main_prod_id = ''
OR mt.main_prod_id IS NULL)
THEN
mt.product_id
ELSE mt.main_prod_id
END) ASC , (CASE
WHEN
mt.product_id NOT IN (SELECT
main_prod_id
FROM
my_table)
AND (mt.main_prod_id = ''
OR mt.main_prod_id IS NULL)
THEN
0
WHEN
mt.product_id IN (SELECT
main_prod_id
FROM
my_table)
AND (mt.main_prod_id = ''
OR mt.main_prod_id IS NULL)
THEN
1
ELSE 2
END) ASC , mt.location ASC
But with the above query what I'm getting is the first table. 但是通过上面的查询,我得到的是第一个表。
Could you give this a try? 你可以试试看吗?
SELECT
product_id, main_prod_id, location
FROM
my_table
ORDER BY
CONCAT(main_prod_id,product_id), location;
Comments: 评论:
This might not the final solution, I haven't tested it, but it might give you some hints. 这可能不是最终的解决方案,我尚未测试过,但可能会给您一些提示。
You didn't sort out your sorting conditions properly before your started writing the query. 在开始编写查询之前,您没有正确地分类排序条件。
You cannot do, what you tried to do, in the ORDER BY
expressions. 您不能在
ORDER BY
表达式中执行尝试执行的操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.