如何在mysql

时间:2018-06-05 18:45:33

标签: mysql

我有一个名为perform的表,其中包含pid(表演者的id)和vid(视频' s id),如下所示:

+---------+------+
| pid     | vid  | 
+---------+------+
| 2303485 | 2    | 
| 2305672 | 2    |
|  ...    | ...  |
+---------+------+

我的问题是,根据视频的ID,如何在给定视频中获取演员执行的所有视频。

我的解决方案是:

select vid 
  from perform 
 where pid in (select pid from perform where vid = 2)

然而,这需要30秒甚至更长时间。我该如何改进?

1 个答案:

答案 0 :(得分:0)

试试这个:

SELECT vid FROM perform WHERE pid IN (SELECT pid FROM perform WHERE vid = 2)

或者这个:

SELECT p1.vid
FROM perform AS p1
INNER JOIN perform AS p2 ON p2.pid = p1.pid AND p2.vid = 2
GROUP BY p1.vid