合并2请求SQL



我有一个问题,当我想合并2个SQL查询。

确实我有一个错误&;#1241 -操作数应该包含1列&;

我不明白为什么我有这个消息,因为我在"IN"后面有1行。

1 -查询子类别空(子类别没有任何博客文章):

SELECT category.id, category.name, category.parent, article.id 
FROM category 
INNER JOIN article 
WHERE (article.category != category.id AND category.parent IS NOT NULL) 
GROUP BY category.id

2 -查询不包含子类别的所有类别

SELECT * 
FROM category 
WHERE id NOT IN ( SELECT parent 
FROM category 
WHERE parent IS NOT NULL) 
AND parent IS NULL

3 -合并查询:

SELECT category.id, category.name, category.parent, article.id 
FROM category 
INNER JOIN article 
WHERE (article.category != category.id 
AND category.parent IS NOT NULL) OR (SELECT * 
FROM category 
WHERE id NOT IN (
SELECT parent 
FROM category 
WHERE parent IS NOT NULL) AND parent IS NULL) 
GROUP BY category.id

我试过用逗号,但它也不起作用。

这个合并的查询我必须有一个列表的类别不子类+子类不博客文章。

SQL图

谢谢你的帮助,Cyril。

你可以试试吗

---
--- 1 - Query "SUB CATEGORY EMPTY" (sub category have not any blog post):
---
select
category.id, 
category.name, 
category.parent, 
article.id 
from 
category left join article 
on (category.id = article.category)
where
article.id is null
and category.parent is not null
group by
category.id, 
category.name, 
category.parent, 
article.id 

---
--- 2 - Query "All categories without any sub-category"
---
select
category.id, 
category.name, 
category.parent, 
null as article_id
from
category
where
category.id not in (select parent from category where parent is not null group by parent )
and category.parent is null
---
--- 3 - Merged queries:
---
select
category.id, 
category.name, 
category.parent, 
article.id 
from 
category left join article 
on (category.id = article.category)
where
article.id is null
and category.parent is not null
group by
category.id, 
category.name, 
category.parent, 
article.id 
union all
select
category.id, 
category.name, 
category.parent, 
null as article_id
from
category
where
category.id not in (select parent from category where parent is not null group by parent )
and category.parent is null

相关内容

  • 没有找到相关文章

最新更新