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?

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

You should look into JOINs to do this with one query (which massivly increases speed compared to fetching the data seperately).

$conn = new PDO(CONNECTION_DETAILS, $username, $pass);
$variable = 1;
$sql = "SELECT game_follower.game_id FROM game_follower ";
$sql .= "INNER JOIN videos ";
$sql .= "ON game_follower.game_id = videos.game_id ";
$sql .= "WHERE game_follower.user_id = :id";
$query = $conn->prepare($sql);
$query->bindParam(':id', $variable, PDO::PARAM_INT);
$query->execute();

Something like that?