[英]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.