dongyange1101 2015-06-27 08:10
浏览 160

ORDER BY在另一个表中找到的大多数行

I have 2 tables, 1 called googleimage and 1 called googleimagefound, googleimagefound has thousands of rows and each row has an image_id column which corresponds to its respective image from googleimage table (id), each image from the googleimage table could have thousands of rows corresponding to it from the googleimagefound table.

What i am trying to do is SELECT all the images from googleimage table and then ORDER it by the amount of rows found in the googleimagefound table.

Example,

googleimage table has 3 rows with the id's 1, 2, 3

googleimagefound table has 100 rows, 50 of them have the image_id 1, 20 of them have the image_id, 2, and 30 of them have the image_id 3

I want to say

SELECT * FROM googleimage ORDER BY most rows found in googleimagefound table where id = googleimagefound.image_id

Any help will be appriciated

  • 写回答

4条回答 默认 最新

  • duandai6373 2015-06-27 08:17
    关注

    Try this way:

    SELECT g.*,T1.Found
    FROM googleimage g JOIN
    (SELECT image_id,COUNT(image_id) as Found
     FROM googleimagefound
     GROUP BY image_id) T1 ON g.image_id=T1.image_id
    ORDER BY T1.Found DESC
    

    Result will be like:

    id     name    others     Found
    --------------------------------
    1      NameA   Value1     120
    5      NameD   Value4     95
    3      NameB   Value2     82
    
    评论

报告相同问题?

悬赏问题

  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装
  • ¥40 复杂的限制性的商函数处理