dsvq5069 2015-08-08 16:30
浏览 36
已采纳

mysql从同一个表上的2个查询获得不同顺序的混合结果

thanks for dedicating time.

I have two tables for a kind of social network, table A for contents and table B for likes:

TABLE A:

CREATE TABLE `A` (
`id` int(10) unsigned NOT NULL,
  `id_user` int(10) unsigned NOT NULL,
  `title` varchar(255) DEFAULT NULL,
  `path` text,
  `ext` varchar(15) DEFAULT NULL,
  `image_type` varchar(100) DEFAULT NULL,
  `link_url` text,
  `creation_date` datetime DEFAULT NULL,
  `size` float DEFAULT NULL,
  `type` int(10) unsigned DEFAULT NULL,
  `number_share` int(10) unsigned NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


ALTER TABLE `A`
 ADD PRIMARY KEY (`id`), ADD KEY `id_user` (`id_user`);

And TABLE B:

CREATE TABLE B (
  `id_b` int(10) unsigned NOT NULL,
  `id_user` int(10) unsigned NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


ALTER TABLE `B`
 ADD PRIMARY KEY (`id_b`,`id_user`), ADD KEY `id_user` (`id_user`);

I'd like to retrieve from mysql records with this order:

    1st most popular 
     1st most recent
     2nd most popular 
     2nd most recent



    .....

Can anyone tell me how to combine this by mysql (if possibile) or something easy to use in php wich is not 2 different queries?

EDIT 1:

If i have this situation:

results from first select (popular): id 3 5 12 4 65 2

and

results from second select (recent):

id 
100 
121 
3 

i will go to print my elements width id:

id
3 
100 
5 
121 
12 

i want that id=3 from the second query is not considered because already posted

Thanks!

  • 写回答

1条回答 默认 最新

  • doumeng1897 2015-08-09 07:39
    关注

    plan

    • use variable, and order by to rank popularity
    • use variable, and order by to rank recent
    • union all
    • re order by rnk, cat
    • group by id to restrict to first id

    input [sample]

    +----+---------+--------+--------+--------+------------+----------+--------------------------+--------+--------+--------------+
    | id | id_user | title  |  path  |  ext   | image_type | link_url |      creation_date       |  size  |  type  | number_share |
    +----+---------+--------+--------+--------+------------+----------+--------------------------+--------+--------+--------------+
    |  1 |      22 | (null) | (null) | (null) | (null)     | (null)   | August, 09 2015 10:54:57 | (null) | (null) |            0 |
    |  2 |      33 | (null) | (null) | (null) | (null)     | (null)   | August, 09 2015 07:54:57 | (null) | (null) |            0 |
    |  3 |      44 | (null) | (null) | (null) | (null)     | (null)   | August, 09 2015 08:54:57 | (null) | (null) |            0 |
    +----+---------+--------+--------+--------+------------+----------+--------------------------+--------+--------+--------------+
    
    +------+---------+
    | id_b | id_user |
    +------+---------+
    |    3 |       8 |
    |    3 |       9 |
    |    3 |      21 |
    |    3 |      22 |
    |    1 |      55 |
    |    3 |      55 |
    |    1 |      66 |
    |    1 |      77 |
    +------+---------+
    

    query

    set @rnk_pop := 0;
    set @rnk_rec := 0;
    
    -- popularity ranking
    select cat, id, rnk
    from
    (
    select cat, id, rnk
    from
    (
      select cat, id, occurs, @rnk_pop := @rnk_pop + 1 as rnk
      from
      (
        select 'popularity' as cat, A.id, count(B.id_b) as occurs
        from A
        left join B
        on B.id_b = A.id
        group by A.id
        order by occurs desc
      ) q1
    ) pop_sort
    union all
    -- recent ranking
    select cat, id, rnk
    from
    (
      select 'recent' as cat, id, @rnk_rec := @rnk_rec + 1 as rnk
      from A
      order by creation_date desc
    ) rec_sort
    order by rnk, cat
    ) all_q
    group by id
    order by rnk, cat
    ;
    

    output

    +------------+----+-----+
    |    cat     | id | rnk |
    +------------+----+-----+
    | popularity |  3 |   1 |
    | recent     |  1 |   1 |
    | popularity |  2 |   3 |
    +------------+----+-----+
    

    sqlfiddle

    notes

    ranking output above would be used to join to display whatever information is useful about the content..

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问如何在openpcdet上对KITTI数据集的测试集进行结果评估?
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错