gpt4 book ai didi

mysql - 如何缩短这个sql查询

转载 作者:行者123 更新时间:2023-11-30 21:51:23 30 4
gpt4 key购买 nike

我在缩短此 sql 代码时遇到问题:

SELECT
(SELECT name FROM content_categories ccb WHERE ccb.id = (SELECT parent FROM content_categories cc WHERE cc.id = 57)) AS parent,
(SELECT id FROM content_categories ccb WHERE ccb.id = (SELECT parent FROM content_categories cc WHERE cc.id = 57)) AS parent_id,
(SELECT alias FROM content_categories ccb WHERE ccb.id = (SELECT parent FROM content_categories cc WHERE cc.id = 57)) AS parent_alias,
(SELECT name as subcategory FROM content_categories ccb WHERE ccb.id = 57) as sub,
(SELECT id as subcategory FROM content_categories ccb WHERE ccb.id = 57) as sub_id,
(SELECT alias as subcategory FROM content_categories ccb WHERE ccb.id = 57) as sub_alias

你能帮我出出主意吗?

最佳答案

我想你只是想要一个LEFT JOIN:

SELECT cp.name as parent, cp.id as parent_id, cp.alias as parent_alias,
cc.name as sub, cc.id as sub_id, cc.alias as sub_alias
FROM content_categories cc left join
content_categories cp
on ccp.id = cc.parent
WHERE cc.id = 57;

唯一的区别是,如果 cc.id = 57 不存在,这将不返回任何行。您的版本将返回一行,其中所有列都是 NULL

关于mysql - 如何缩短这个sql查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47113038/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com