Mixing different categories results, ordered by score in MySQL

后端 未结 3 1363
栀梦
栀梦 2021-01-14 11:53

In my PHP application, I have a mysql table of articles which has the following columns:

article_id    articletext    category_id    score

3条回答
  •  攒了一身酷
    2021-01-14 11:56

    Just for learning purpose. I made a test with 3 categories. I have no idea how this query could run on a large recordset.

    select * from (
    (select @r:=@r+1 as rownum,article_id,articletext,category_id,score
    from articles,(select @r:=0) as r
    where category_id = 1
    order by score desc limit 100000000) 
    union all
    (select @r1:=@r1+1,article_id,articletext,category_id,score
    from articles,(select @r1:=0) as r
    where category_id = 2
    order by score desc limit 100000000)
    union all
    (select @r2:=@r2+1,article_id,articletext,category_id,score
    from articles,(select @r2:=0) as r
    where category_id = 3
    order by score desc limit 100000000)
    ) as t
    order by rownum,score desc
    

提交回复
热议问题