[英]how to get related post comparing their json tags
I want to get 4
related posts with a current one, comparing their tags. 我想拿4
相关的最新文章,比较它们的标签。
Number of tags per post can vary but max - 5; 每个帖子的标签数量可以变化,但最多-5;
Tags are stored inside JSON
column as an array, like this: 标签以数组的形式存储在JSON
列中,如下所示:
["ABBA","SKY","BERN"]
["ALPHA","SKY","SEA", "ABBA"]
["VENUS","EARTH"]
["ABBA","AMSTEL"]
Now suppose the current post has this tags: 现在,假设当前帖子具有以下标签:
["ABBA","SKY","BERN"] // row-id = 0
I want to get row id
of related tags. 我想获取相关标签的row id
。
In the above example result should be: 在上面的示例中,结果应为:
row-id = 1 // because of `SKY` and `ABBA`;
row-id = 3 // because of `ABBA`;
... ans so on untill 4 related rows are selected, ordered by date desc
. ...等等,直到选择了4个相关行,并按date desc
排序。
Something like this: 像这样:
select id, img, tags from posts
where id <> $currentId
and tags contains any of $current_tags
limit 4
order by date desc.
Any help? 有什么帮助吗?
You can make a selection like this: 您可以像这样进行选择:
SELECT id, img, tags FROM posts
WHERE tags LIKE '%"ABBA"%' OR tags LIKE '"%SKY%"' OR tags LIKE '"%BERN%"'
LIMIT 4
ORDER BY date DESC;
In PHP: 在PHP中:
$query = "SELECT id, img, tags FROM posts
WHERE tags LIKE '%".implode("%' OR tags LIKE '%", $yourInputArray)."%'
LIMIT 4
ORDER BY date DESC;";
Where: 哪里:
$yourInputArray = array("ABBA","SKY","BERN");
Since you will be having a max of 5 tags, and assuming JSON_SEARCH is available, you could try the following, though it is a bit long. 由于最多将有5个标签,并且假设JSON_SEARCH可用,因此可以尝试以下操作,尽管它有点长。
JSON_SEARCH will return null if any of the 0 to 4 indices do not exist. 如果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.