This question already has an answer here:
I honestly don't know what exactly to put in the title, but here goes the problem:
I have table(movie_actor) referring to actors and movies they are related with. Something like:
AID | MID
AC01 | MV1
AC02 | MV1
AC01 | MV2
AC02 | MV3
AC01 | MV3
So I have come up on how to get for example all movies relating to MV1 through its actors through this sql query:
SELECT movie_actor.MID
FROM movie_actor
WHERE AID
IN ( SELECT AID FROM movie_actor WHERE IN MID = "MV1") AND movie_actor.MID<>"MV1"
that would result to:
MID: MV2, MV3
Now what I really want to get is this related movies to MV1 and their corresponding number of related movies. Basically:
MID | Number of Related Movies
MV2 | 2 (MV1, MV3)
MV3 | 2 (MV1, MV2)
Now I can get this using count(*) and the query I mentioned earlier.
SELECT COUNT(*)
FROM movie_actor
WHERE AID
IN (
SELECT AID
FROM movie_actor
WHERE MID="MV2")
But I have to do so individually which I can do away with php(tried it,slow with large amount of data). Is it possible to do it in one query?
also if i am able to do this, will it be faster as oppose to doing it in php?
sorry if I have weird formatting, new here :) also pardon the noobness
</div>
SELECT movie_actor.MID FROM movie_actor WHERE AID IN ( SELECT AID FROM movie_actor WHERE IN MID = "MV1") group by MID