仅当 parent 存在时加入 parent 名称
Join parent name only if parent exists
仅当 parent 存在时,我才需要将 parent 个名称加入到 child 的行中,否则不要将 parent 个名称加入到该行中。
因此,我需要有一行 child 有或没有 parent 个名字。
我该如何实现?
SELECT t.*, cat.name AS cat_name
FROM products AS t
INNER JOIN category AS cat
ON category_id=cat.id
WHERE t.is_public!=2
只需将您的 INNER JOIN
替换为 LEFT JOIN
,所有产品都将包含在结果集中,对于没有 [=17] 的产品,cat_name
为 NULL
=].
仅当 parent 存在时,我才需要将 parent 个名称加入到 child 的行中,否则不要将 parent 个名称加入到该行中。
因此,我需要有一行 child 有或没有 parent 个名字。
我该如何实现?
SELECT t.*, cat.name AS cat_name
FROM products AS t
INNER JOIN category AS cat
ON category_id=cat.id
WHERE t.is_public!=2
只需将您的 INNER JOIN
替换为 LEFT JOIN
,所有产品都将包含在结果集中,对于没有 [=17] 的产品,cat_name
为 NULL
=].