Den här frågan returnerar alla objekt som har det maximala antalet taggar gemensamma:
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 fiolen här .
Eller så kommer den här att returnera alla artiklar, även de som inte har några gemensamma taggar, sorterade efter antalet taggar i gemensam beskrivning:
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;