I need a query that find the recommended TV shows for an user, based on the TV Shows he is following. Do to this I have the following tables:
the table
Progressthat contains wich show the user is following and the percentage of seen episodes (to solve this problem we can assume I have only one user in the database)the table
Suggestedthat contains_id1,_id2andvalue(value is the strength of the connections between the show with id=_id1and the show with id=_id2: the morevalueis great, the more the shows have something in common). Note that in this table applies the commutative property, so the strength of the connection betweenid1and_id2is the same of_id1and_id2. Moreover there aren't two rows such as ROW1._id1=ROW2._id2 AND ROW1._id2 = ROW2._id1the table
ShowCachethat contains the details about a TV Show, such as name etc..
The following query is what I'm trying to do, but the result is an empty set:
SET @a = 0; //In other tests this line seem to be necessary
SELECT `ShowCache`.*,
(SUM(value) * (Progress.progress)) as priority
FROM `Suggested`,`ShowCache`, Progress
WHERE
((_id2 = Progress.id AND _id1 NOT IN (SELECT id FROM Progress) AND @a:=_id1)//There is a best way to set a variable here?
OR
(_id1 = Progress.id AND _id2 NOT IN (SELECT id FROM Progress) AND @a:=_id2))
AND `ShowCache`._id = @a //I think that the query fails here
GROUP BY `ShowCache`._id
ORDER BY priority DESC
LIMIT 0,20
I know the problem is related to the usage of variables, but I can't solve it. Any help is really appreciated.
PS: the main problem is that (because of the commutative propriety), without variables I need two queries, wich takes about 3 secs to begin executed (the query is more complex than the above). I'm really trying to make a single query to do this task
PPS: I tied also with an XOR operation, that results in an infinite loop?!?!? Here's the WHERE clause I tried:
((_id2=Progress.id AND @a:=_id1) XOR (_id1=Progress.id AND @a:=_id2)) AND `ShowCache`._id = @a
EDIT: I come up with this WHERE conditions without using any variable:
(_id2 = Progress.id OR _id1 = Progress.id)
AND `ShowCache`._id = IF(_id2 = Progress.id, _id1,_id2)
AND `ShowCache`._id NOT IN (SELECT id FROM Progress)
It works, but it is very slow.