Esta consulta retornará todos os itens que possuem o número máximo de tags em comum:
SET @item = 1;
SELECT
goods_id
FROM
links
WHERE
tag_id IN (SELECT tag_id FROM links WHERE [email protected])
AND [email protected]
GROUP BY
goods_id
HAVING
COUNT(*) = (
SELECT
COUNT(*)
FROM
links
WHERE
tag_id IN (SELECT tag_id FROM links WHERE [email protected])
AND [email protected]
GROUP BY
goods_id
ORDER BY
COUNT(*) DESC
LIMIT 1
)
Por favor, veja violino aqui .
Ou este retornará todos os itens, mesmo aqueles sem tags em comum, ordenados pelo número de tags em common desc:
SELECT
goods_id
FROM
links
WHERE
[email protected]
GROUP BY
goods_id
ORDER BY
COUNT(CASE WHEN tag_id IN (SELECT tag_id FROM links WHERE [email protected]) THEN 1 END) DESC;