Denne forespørgsel returnerer alle elementer, der har det maksimale antal tags til fælles:
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
)
Se venligst violin her .
Eller denne vil returnere alle varer, selv dem uden fælles tags, sorteret efter antallet af fælles tags:
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;