Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用php/mysql按标题和标记显示相关文章_Php_Mysql - Fatal编程技术网

使用php/mysql按标题和标记显示相关文章

使用php/mysql按标题和标记显示相关文章,php,mysql,Php,Mysql,我有一个像这样的文章和标签表 1. articles |itemid |title |tag 1 |my first post |hello, world 2 |my second |world war post 3 |my third post |testing the tag is separate either by space or comma 2. tagterm tag_id|

我有一个像这样的文章和标签表

 1. articles
|itemid   |title            |tag
1         |my first post    |hello, world
2         |my second        |world war post
3         |my third post    |testing

the tag is separate either by space or comma

2. tagterm
tag_id| tagterm
1     | hello
2     | world
3     | war
4     | post
5     | testing


3. tag_link
id| tag_id| tag_itemid
1 | 1     | 1
2 | 2     | 1
3 | 2     | 2
and so on 
如果可能的话,我想通过标签字段和标题显示所有相关文章

到目前为止我所尝试的。。。不工作请帮忙,我是新手

SELECT a1.itemid, GROUP_CONCAT(DISTINCT a2.itemid) AS related_articles 
FROM articles AS a1 
JOIN tag_link AS t1 ON a1.itemid = t1.tag_itemid 
JOIN articles AS a2 ON a2.itemid = t1.tag_itemid GROUP BY a1.itemid
试试看:

SELECT a1.itemid, a1.title, group_concat(DISTINCT a2.itemid, CONCAT('@',a2.title) SEPARATOR "||") as related_articles
FROM articles AS a1 
JOIN tag_link AS t1 ON a1.itemid = t1.tag_itemid 
JOIN 
    (SELECT a1.itemid as itemid, a1.title as title, t1.tag_id as tag_id
        FROM articles AS a1 
        JOIN tag_link AS t1 ON a1.itemid = t1.tag_itemid) as `a2`
    ON a2.tag_id=t1.tag_id
WHERE a2.itemid != a1.itemid
GROUP BY a1.itemid

Live

不起作用不是一个好的问题描述。你得到了什么结果?你期望得到什么样的结果?