このクエリは、共通のタグの最大数を持つすべてのアイテムを返します:
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
)
フィドル
または、これは、共通のタグがないものも含めて、共通の説明のタグの数順に並べられたすべてのアイテムを返します。
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;