简体   繁体   English

更改列值取决于出现次数

[英]Change the column value depends on occurrence count

There is the table:有表:

NAME   GENRE
book1  genre1
book2  genre2
book3  genre1
book3  genre2
book4  genre1
etc

So, we can have a book with only one or with many genres.因此,我们可以拥有一本只有一种或多种类型的书。 From this table I would like to obtain this table从这张表我想得到这张表

NAME   GENRE
book1  genre1
book2  genre2
book3  mixed_genres
book4  genre1

So in the new table if the book has only one genre, we keep this genre, if the book has many genres we change genre value by “mixed_genres” .所以在新表中,如果书只有一种流派,我们保留这个流派,如果书有很多流派,我们通过“mixed_genres”改变流派值

There is any way to do this?有没有办法做到这一点? Many thanks for any advice!非常感谢您的任何建议!

You can use aggregation:您可以使用聚合:

select book,
       (case when min(genre) = max(genre) then max(genre)
             else 'mixed genres'
        end) as genre
from t
group by book;

You can also list out all the genres:您还可以列出所有类型:

select book, group_concat(genre) as genre
from t
group by book;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM