Kueri ini akan mengembalikan semua item yang memiliki jumlah maksimum tag yang sama:
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
)
Silakan lihat biola di sini .
Atau yang ini akan mengembalikan semua item, bahkan yang tidak memiliki tag yang sama, diurutkan berdasarkan jumlah tag yang sama 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;