dsfdfd1211 2012-07-21 14:08
浏览 330
已采纳

MYSQL - 如何从第一个表返回数组,并在一个查询中使用该数组从另一个表中选择?

I have 2 tables:

1) game_follower (stores which user is following which game):

___________________________
| ID  | game_id | user_id |
| 1   |  1      |  1      |
| 2   |  2      |  1      |

2) videos:

__________________________________
| ID  | game_id | content | data |
| 1   |  2      |  blah   | blah |
| 2   |  1      |  blah   | blah |

I'm trying to implement a twitter-style following system, but I'm having trouble writing a query that does the following:

  • select multiple game_ids WHERE user_id = "1"
  • from that list of game_ids, select from videos where game_id = (multiple game ids that have been selected from the first query)

I can do it with separate queries but I've been told that using multiple queries is bad for performance, so how do I do this in with one query?

  • 写回答

3条回答 默认 最新

  • dounuo9921 2012-07-21 14:12
    关注

    try this

    SELECT v.game_id
    FROM videos v
    INNER JOIN game_follower gf ON gf.game_id = v.game_id
    WHERE gf.user_id = 1
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助