Php 如何通过比较json标记获得相关帖子
我想获得与当前帖子相关的Php 如何通过比较json标记获得相关帖子,php,mysql,json,Php,Mysql,Json,我想获得与当前帖子相关的4篇,比较它们的标签 每个帖子的标签数量可能有所不同,但最多为-5个 标记作为数组存储在JSON列中,如下所示: ["ABBA","SKY","BERN"] ["ALPHA","SKY","SEA", "ABBA"] ["VENUS","EARTH"] ["ABBA","AMSTEL"] SELECT id, img, tags FROM posts WHERE tags LIKE '%"ABBA"%' OR tags LIKE '"%SKY%"' OR tag
4篇
,比较它们的标签
每个帖子的标签数量可能有所不同,但最多为-5个
标记作为数组存储在JSON
列中,如下所示:
["ABBA","SKY","BERN"]
["ALPHA","SKY","SEA", "ABBA"]
["VENUS","EARTH"]
["ABBA","AMSTEL"]
SELECT id, img, tags FROM posts
WHERE tags LIKE '%"ABBA"%' OR tags LIKE '"%SKY%"' OR tags LIKE '"%BERN%"'
LIMIT 4
ORDER BY date DESC;
现在假设当前帖子有以下标记:
["ABBA","SKY","BERN"] // row-id = 0
我想获取相关标记的行id
在上述示例中,结果应为:
row-id = 1 // because of `SKY` and `ABBA`;
row-id = 3 // because of `ABBA`;
。。。ans等,直到选择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.
从帖子中选择id、img、标签
其中id$currentId
和标记包含任何$current\u标记
限制4
按日期说明订购。
有什么帮助吗?您可以做出如下选择:
["ABBA","SKY","BERN"]
["ALPHA","SKY","SEA", "ABBA"]
["VENUS","EARTH"]
["ABBA","AMSTEL"]
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_搜索可用,您可以尝试以下方法,尽管有点长 如果0到4个索引中的任何一个不存在,JSON_搜索将返回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;
我明白你的想法,但我认为你在第二行
PHP
中漏掉了“beforeinfrade
这样的标签。对不起,我已经修复了……”