duanchuang1935 2011-09-13 20:55
浏览 45
已采纳

困难的SQL查询

i have 2 tables i.e. table 1 calls game and table 2 calls game_medias

structure table 1 (game)

-id
-genre_id
-title
-created

etc.

structure table 2 (game_medias)

-id
-game_id
-thumb

etc

example query:

select g.id,g.genre_id,g.title, gm.* 
from games g inner join game_medias as gm 
group by rand(g.id) limit 8;

is it possible to make a query with a extra like " where g.id = '2' " - get the genre_id from it and get into the random 8 only "items" with this genre_id ? i'm not sure if the join is the right solution - maybe someone knows a better way?

regards

  • 写回答

1条回答 默认 最新

  • douraoyw194498 2011-09-13 21:01
    关注

    There are many ways to do this but I believe this will work:

    select g.id,g.genre_id,g.title, gm.* 
    from games g inner join game_medias as gm 
    where (g.genre_id = (SELECT genre_id FROM games WHERE id = 2))
    group by rand(g.id) limit 8;
    

    If there is an index on games.id then it should be quite fast.

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

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题