为sql查询中的列设置默认值



我有一个数据库,其中第一个表是"文章"表(id, body…)和第二个包含单词(word_id, word)的表。这些词就像是文章的标签/主题

另一个表连接前面的表:article_labels(article_id, word_id)。

有些文章没有标签(不包括在article_labels表中),大多数文章有多个标签。

我所拥有的是一个查询来获取标签文章(id | body | label1/label2/…)

select  article_id, body
group_concat(word SEPARATOR '/') AS labels 
from article_labels l,
portfolio_2022.words w,
articles a
where a.language='en'
and l.word_id = w.id
and a.id = l.article_id 
group by article_id;

我想做的是获取所有带有标签的文章,如果一篇文章没有被标记为默认值(e。

提前感谢!

您可以使用子查询:

select id, body, coalesce((
select group_concat(words.word separator '/')
from article_labels
join words on article_labels.word_id = words.id
where article_labels.article_id = articles.id 
), 'unlabeled') as labels
from articles
where language = 'en'

最新更新