[英]how to get related post comparing their json tags
我想拿4
相關的最新文章,比較它們的標簽。
每個帖子的標簽數量可以變化,但最多-5;
標簽以數組的形式存儲在JSON
列中,如下所示:
["ABBA","SKY","BERN"]
["ALPHA","SKY","SEA", "ABBA"]
["VENUS","EARTH"]
["ABBA","AMSTEL"]
現在,假設當前帖子具有以下標簽:
["ABBA","SKY","BERN"] // row-id = 0
我想獲取相關標簽的row id
。
在上面的示例中,結果應為:
row-id = 1 // because of `SKY` and `ABBA`;
row-id = 3 // because of `ABBA`;
...等等,直到選擇了4個相關行,並按date desc
排序。
像這樣:
select id, img, tags from posts
where id <> $currentId
and tags contains any of $current_tags
limit 4
order by date desc.
有什么幫助嗎?
您可以像這樣進行選擇:
SELECT id, img, tags FROM posts
WHERE tags LIKE '%"ABBA"%' OR tags LIKE '"%SKY%"' OR tags LIKE '"%BERN%"'
LIMIT 4
ORDER BY date DESC;
在PHP中:
$query = "SELECT id, img, tags FROM posts
WHERE tags LIKE '%".implode("%' OR tags LIKE '%", $yourInputArray)."%'
LIMIT 4
ORDER BY date DESC;";
哪里:
$yourInputArray = array("ABBA","SKY","BERN");
由於最多將有5個標簽,並且假設JSON_SEARCH可用,因此可以嘗試以下操作,盡管它有點長。
如果0到4索引中的任何一個都不存在,則JSON_SEARCH將返回null。
SELECT id, img, tags
FROM posts
WHERE (JSON_SEARCH(@current_tags, 'one', tags ->> '$[0]') is not null
OR JSON_SEARCH(@current_tags, 'one', tags ->> '$[1]') is not null
OR JSON_SEARCH(@current_tags, 'one', tags ->> '$[2]') is not null
OR JSON_SEARCH(@current_tags, 'one', tags ->> '$[3]') is not null
OR JSON_SEARCH(@current_tags, 'one', tags ->> '$[4]') is not null)
LIMIT 4
ORDER BY DATE desc;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.