dongshi6844 2013-11-16 11:29
浏览 20
已采纳

选择所有项目,按使用项目的次数排序

This query makes sense in my head but i've spent ages trying to write it and not getting any where. I'm using Codeigniter and it's Active record function, but any standard MYSQL would do.

I have a list of venues, and a list of events. A venue can have a number of events at it, and each events stores which venue it is at.

I want to run a query that fetches all venues, and orders them by the number of events they have.

So the first in the list will be the venue with 20 events, the last the venues with 0 events.

Can anybody help with the query? Table structure is simply;

Venues(id, name, address)
Events(id, venue_id, event_name)

Any help would be greatly appreciated.

Thanks

  • 写回答

1条回答 默认 最新

  • duanqu9292 2013-11-16 11:37
    关注

    something like this?:

    SELECT v.id, COUNT(*)
    FROM Venues v, Events e
    WHERE v.id = e.id
    GROUP BY v.id
    ORDER BY COUNT(*) DESC;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误