SQL 在 Google Cloud Spanner 中返回嵌套类别的查询?
SQL query for returning nested categories in Google Cloud Spanner?
我在 Google Cloud Spanner 中有以下 table 类别。
CREATE TABLE categories (
categoryId STRING(36) NOT NULL,
name STRING(128) NOT NULL,
parent BOOL NOT NULL,
parentId STRING(36),
archived BOOL,
FOREIGN KEY (parentId) REFERENCES categories (categoryId)
) PRIMARY KEY(categoryId);
因此 table 可能具有如下所示的条目。
categoryId
name
parent
parentId
archived
1
Clothing
true
false
2
Homeware
true
false
3
Shirts
false
1
false
4
Pants
false
1
false
5
Technology
true
false
6
Tablets
false
5
true
我想查询这个 table 以便它 return 首先是所有未存档的 parent 类别(我不能真正删除我的应用程序中的行,因为文档将引用行 - 所以我实施了归档),以及属于所述 parents.
的所有 child 类别(未归档)
我编写了以下查询:
SELECT categories.categoryId, categories.name, childCategories.name AS childName
FROM categories
LEFT JOIN categories AS childCategories
ON childCategories.parentId = categories.categoryId
WHERE categories.parent = true AND categories.archived = FALSE
但是,这将归档 return 行 child 类别。下面是确切的结果 returned.
categoryId
name
childName
1
Clothing
Shirts
1
Clothing
Pants
2
Homeware
5
Technology
Tablets
期望的结果是:
categoryId
name
childName
1
Clothing
Shirts
1
Clothing
Pants
2
Homeware
5
Technology
我尝试将查询更改为:
SELECT categories.categoryId, categories.name, childCategories.name AS childName
FROM categories
LEFT JOIN categories AS childCategories
ON childCategories.parentId = categories.categoryId
WHERE categories.parent = true AND categories.archived = FALSE AND childCategories.archived = FALSE
但是,如果它们没有至少一个 child 类别,则不会 return 任何 parent 类别。下面是确切的结果 returned.
categoryId
name
childName
1
Clothing
Shirts
1
Clothing
Pants
我不知道如何编写查询以获得所需的结果。任何 help/insight 将不胜感激。提前致谢!
您几乎完成了,因为您只需要未归档的子类别,将条件 childCategories.archived = FALSE
移至 ON
而不是 WHERE
子句。
将过滤器放在 WHERE
上将从数据集中删除结果,但是如果将其置于 ON
条件下,将 return 来自父类别的所有内容和来自子类别的相应匹配行.
SELECT categories.categoryId, categories.name, childCategories.name AS childName
FROM categories
LEFT JOIN categories AS childCategories
ON childCategories.parentId = categories.categoryId
AND childCategories.archived = FALSE --Added here
WHERE categories.parent = true AND categories.archived = FALSE
我在 Google Cloud Spanner 中有以下 table 类别。
CREATE TABLE categories (
categoryId STRING(36) NOT NULL,
name STRING(128) NOT NULL,
parent BOOL NOT NULL,
parentId STRING(36),
archived BOOL,
FOREIGN KEY (parentId) REFERENCES categories (categoryId)
) PRIMARY KEY(categoryId);
因此 table 可能具有如下所示的条目。
categoryId | name | parent | parentId | archived |
---|---|---|---|---|
1 | Clothing | true | false | |
2 | Homeware | true | false | |
3 | Shirts | false | 1 | false |
4 | Pants | false | 1 | false |
5 | Technology | true | false | |
6 | Tablets | false | 5 | true |
我想查询这个 table 以便它 return 首先是所有未存档的 parent 类别(我不能真正删除我的应用程序中的行,因为文档将引用行 - 所以我实施了归档),以及属于所述 parents.
的所有 child 类别(未归档)我编写了以下查询:
SELECT categories.categoryId, categories.name, childCategories.name AS childName
FROM categories
LEFT JOIN categories AS childCategories
ON childCategories.parentId = categories.categoryId
WHERE categories.parent = true AND categories.archived = FALSE
但是,这将归档 return 行 child 类别。下面是确切的结果 returned.
categoryId | name | childName |
---|---|---|
1 | Clothing | Shirts |
1 | Clothing | Pants |
2 | Homeware | |
5 | Technology | Tablets |
期望的结果是:
categoryId | name | childName |
---|---|---|
1 | Clothing | Shirts |
1 | Clothing | Pants |
2 | Homeware | |
5 | Technology |
我尝试将查询更改为:
SELECT categories.categoryId, categories.name, childCategories.name AS childName
FROM categories
LEFT JOIN categories AS childCategories
ON childCategories.parentId = categories.categoryId
WHERE categories.parent = true AND categories.archived = FALSE AND childCategories.archived = FALSE
但是,如果它们没有至少一个 child 类别,则不会 return 任何 parent 类别。下面是确切的结果 returned.
categoryId | name | childName |
---|---|---|
1 | Clothing | Shirts |
1 | Clothing | Pants |
我不知道如何编写查询以获得所需的结果。任何 help/insight 将不胜感激。提前致谢!
您几乎完成了,因为您只需要未归档的子类别,将条件 childCategories.archived = FALSE
移至 ON
而不是 WHERE
子句。
将过滤器放在 WHERE
上将从数据集中删除结果,但是如果将其置于 ON
条件下,将 return 来自父类别的所有内容和来自子类别的相应匹配行.
SELECT categories.categoryId, categories.name, childCategories.name AS childName
FROM categories
LEFT JOIN categories AS childCategories
ON childCategories.parentId = categories.categoryId
AND childCategories.archived = FALSE --Added here
WHERE categories.parent = true AND categories.archived = FALSE