douh9817 2010-04-10 23:34
浏览 91
已采纳

在连接表中找到的MySQL一对多按订单排序

Two tables:

table_a
-------
table_a_id: (primary, int)

table_b
-------
table_a_id: (index, int, from table_a)
table_b_value: (varchar)

A one to many relationship between table_a_id and table_b_value.

Given a query like this:

SELECT DISTINCT(table_a_id) FROM table_a 
JOIN table_b ON table_a.table_a_id=table_b.table_a_id

I want to order by the number of occurrences of table_a_id in table_b. I'm not really sure how to write this in MySQL.

  • 写回答

4条回答 默认 最新

  • douchixu3686 2010-04-10 23:38
    关注
    SELECT COUNT(table_b.table_a_id) AS count 
    FROM table_a 
    JOIN table_b ON table_b.table_a_id = table_a.id 
    GROUP BY table_b.table_a_id 
    ORDER_BY count DESC 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器