MySQL COUNT() multiple columns

前端 未结 3 1066
囚心锁ツ
囚心锁ツ 2021-01-13 16:02

I\'m trying to fetch the most popular tags from all videos in my database (ignoring blank tags). I also need the \'flv\' for each tag. I have this working as I want if each

3条回答
  •  南笙
    南笙 (楼主)
    2021-01-13 16:36

    select tag, flv, count(*) as tag_count
    from (
      select tag_1 as tag, flv from videos
      UNION
      select tag_2 as tag, flv from videos
      UNION
      select tag_3 as tag, flv from videos
    ) AS X
    

    I think that will do it, although there will be double-counting if any record has the same values for two of the tags.

    UPDATE: added AS X.

提交回复
热议问题