你可以按多个字段对去重的 MySQL 数据进行排序,忽略空值。

huangapple go评论59阅读模式
英文:

How can I sort deduplicated mySQL data by multiple fields ignoring nulls?

问题

我试图按正确的顺序获取经siteId去重的类别列表。

数据如下:

类别
=============
类别ID,
siteId,
父类别ID,
标题,
激活

这是我的当前查询:

选择
parentTitle,
childTitle,
subChildTitle,
从 (
    选择      subChild.categoryId as parentCategoryId,
                child.categoryId as childCategoryId,
                parent.categoryId as subChildCategoryId,
                subChild.title as subChildTitle,
                child.title as childTitle,
                parent.title as parentTitle
    从        类别 subChild
    左加入   类别 child on child.categoryId = subChild.parentId 
    左加入   类别 parent on parent.categoryId = child.parentId 
    其中 subChild.siteId 在 (1,2,3)
    和 subChild.active = 'Y'
    ) 作为类别 
按 parentTitle, childTitle, subChildTitle 分组
按 COALESCE(parentTitle, childTitle, subChildTitle) 排序;

我得到的结果如下:

NULL	   NULL	        家电	
NULL	   家电	洗碗机	
NULL	   家电	干衣机
家电      洗碗机       不正确清洗
家电      干衣机        不正确清洗

我想要的是这个:

NULL	   NULL	        家电	
NULL	   家电	洗碗机	
家电      洗碗机       不正确清洗
NULL	   家电	干衣机
家电      干衣机        不正确清洗

更好的是这样:(但只要正确排序就可以)

家电          NULL        NULL
家电          洗碗机      NULL
家电          洗碗机      不正确清洗
家电          干衣机      NULL
家电          干衣机      不正确清洗
英文:

I'm trying to get a list of categories, de-duplicated by siteId in the correct order.

Data looks like this:

categories
=============
categoryId,
siteId,
parentId,
title,
active

Here is my current query:

SELECT
parentTitle,
childTitle,
subChildTitle,
from (
    select      subChild.categoryId as parentCategoryId,
                child.categoryId as childCategoryId,
                parent.categoryId as subChildCategoryId,
                subChild.title as subChildTitle,
                child.title as childTitle,
                parent.title as parentTitle
    from        categories subChild
    left join   categories child on child.categoryId = subChild.parentId 
    left join   categories parent on parent.categoryId = child.parentId 
    where subChild.siteId in (1,2,3)
    and subChild.active = 'Y'
    ) as categories 
group by parentTitle, childTitle, subChildTitle
order by COALESCE(parentTitle, childTitle, subChildTitle);

I get back results like this:

NULL	   NULL	        Appliance	
NULL	   Appliance	Dishwasher	
NULL	   Appliance	Dryer
Appliance  Dishwasher   Not Cleaning Correctly
Appliance  Dryer        Not Cleaning

What i want is this:

NULL	   NULL	        Appliance	
NULL	   Appliance	Dishwasher	
Appliance  Dishwasher   Not Cleaning Correctly
NULL	   Appliance	Dryer
Appliance  Dryer        Not Cleaning

even better would be this: (but i'm fine with it just sorting correct above)

Appliance     NULL        NULL
Appliance     Dishwasher  NULL
Appliance     Dishwasher  Not Cleaning Correctly
Appliance     Dryer       NULL
Appliance     Dryer       Not Cleaning

答案1

得分: 1

以下是翻译好的SQL查询部分:

SELECT
    parentTitle,
    childTitle,
    subChildTitle
FROM (
    SELECT
        COALESCE(parent.title, child.title, subChild.title) AS parentTitle,
        CASE
            WHEN parent.title IS NULL AND child.title IS NULL THEN NULL
            WHEN parent.title IS NULL THEN subChild.title
            ELSE child.title
		END AS childTitle,
        CASE
            WHEN parent.title IS NOT NULL AND child.title IS NOT NULL THEN subChild.title
		END AS subChildTitle
    FROM categories subChild
    LEFT JOIN categories child on child.categoryId = subChild.parentId 
    LEFT JOIN categories parent on parent.categoryId = child.parentId 
    WHERE subChild.siteId IN (1, 2, 3)
    AND subChild.active = 'Y'
) AS c
GROUP BY parentTitle, childTitle, subChildTitle
ORDER BY parentTitle, childTitle, subChildTitle;

请注意,我已经排除了代码部分,只提供了SQL查询的翻译。

英文:

How about this approach which is just reordering the columns a bit:

SELECT
    parentTitle,
    childTitle,
    subChildTitle
FROM (
    SELECT
        COALESCE(parent.title, child.title, subChild.title) AS parentTitle,
        CASE
            WHEN parent.title IS NULL AND child.title IS NULL THEN NULL
            WHEN parent.title IS NULL THEN subChild.title
            ELSE child.title
		END AS childTitle,
        CASE
            WHEN parent.title IS NOT NULL AND child.title IS NOT NULL THEN subChild.title
		END AS subChildTitle
    FROM categories subChild
    LEFT JOIN categories child on child.categoryId = subChild.parentId 
    LEFT JOIN categories parent on parent.categoryId = child.parentId 
    WHERE subChild.siteId IN (1, 2, 3)
    AND subChild.active = 'Y'
) AS c
GROUP BY parentTitle, childTitle, subChildTitle
ORDER BY parentTitle, childTitle, subChildTitle;

Given the following as source data:

categoryId siteId parentId title active
1 1 NULL Appliance Y
2 1 1 Dishwasher Y
3 1 1 Dryer Y
4 1 2 Not Cleaning Correctly Y
5 1 3 Not Cleaning Y
6 2 NULL Appliance Y
7 2 6 Dishwasher Y
8 2 6 Dryer Y
9 2 7 Not Cleaning Correctly Y
10 2 8 Not Cleaning Y
11 3 NULL Appliance Y
12 3 11 Dishwasher Y
13 3 11 Dryer Y
14 3 12 Not Cleaning Correctly Y
15 3 13 Not Cleaning Y

The query returns:

parentTitle childTitle subChildTitle
Appliance NULL NULL
Appliance Dishwasher NULL
Appliance Dishwasher Not Cleaning Correctly
Appliance Dryer NULL
Appliance Dryer Not Cleaning

We cannot be sure how your client application is using this data, but it is worth having a read through this blog post, which may help you look at your adjacency list differently.

huangapple
  • 本文由 发表于 2023年2月24日 07:47:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75551411.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定