Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.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
Mysql 正在尝试联接5个表,出现问题_Mysql_Select - Fatal编程技术网

Mysql 正在尝试联接5个表,出现问题

Mysql 正在尝试联接5个表,出现问题,mysql,select,Mysql,Select,我对mysql和php都相当陌生,所以我仍然在想这一切,所以请耐心听我说 我基本上有一个网站,用户可以在那里制作主题,并为他们的主题添加标记词,我尝试加入表格,这样当我查询帖子时,它也可以在那里显示标记信息 "SELECT u.user_id, u.username, u.profile, topic_tags.tag_id, tags.tag_id, tags.tags, p.post_id, p.post_content, p.post_date, p.post_topic,

我对mysql和php都相当陌生,所以我仍然在想这一切,所以请耐心听我说

我基本上有一个网站,用户可以在那里制作主题,并为他们的主题添加标记词,我尝试加入表格,这样当我查询帖子时,它也可以在那里显示标记信息

"SELECT u.user_id, u.username, u.profile, topic_tags.tag_id, tags.tag_id, tags.tags,
       p.post_id, p.post_content, p.post_date, p.post_topic, p.post_by, p.invisipost 
FROM `posts` p
JOIN `users` u on p.post_by = u.user_id
JOIN `topics` t on p.post_topic = t.topic_id
WHERE p.post_topic='$id'
INNER JOIN `tags` ON topic_tags.tag_id = tags.tag_id
INNER JOIN `topic_tags` ON topics.topic_id = topic_tag.tag_id
WHERE topic_tags.tag_id = topics.topic_id";
就像我说的,我还是个新手,所以如果你能提供任何建议,我将不胜感激

编辑:下面是调用标记的代码

<?php
        $topic_id = $rows['topic_id'];
        $sql=mysql_query("SELECT * FROM topic_tags WHERE `topic_id`='{$topic_id}'");
        while($rowd=mysql_fetch_array($sql))
        {   
            $tag_id = $rowd['tag_id'];
            $fetch_name = mysql_fetch_object(mysql_query("SELECT * FROM `tags` WHERE `tag_id`='{$tag_id}'"));
        ?>
            <div id="topic-tagged"><?php echo ucwords($fetch_name->tags);?></div> 
        <?php
        }       
        ?>

所有的位置都应该在末尾:

SELECT u.user_id, u.username, u.profile, 
topic_tags.tag_id, tags.tag_id, tags.tags,
   p.post_id, p.post_content, p.post_date, 
 p.post_topic, p.post_by, p.invisipost 
FROM `posts` p
JOIN `users` u on p.post_by = u.user_id
JOIN `topics` t on p.post_topic = t.topic_id
INNER JOIN `tags` ON topic_tags.tag_id = tags.tag_id
INNER JOIN `topic_tags` ON topics.topic_id = topic_tag.tag_id
WHERE p.post_topic='$id' and topic_tags.tag_id = topics.topic_id

这是根据您的原始问题更正的查询语句。不过,我仍然不确定最后一部分是否正确。您可能希望直接在数据库中运行此命令,并查看是否得到所需的结果。

编写SQL时有一些规则。
where
子句位于
from
子句之后。此外,表格只有在放入
from
子句后,才能作为
on
子句中的引用。而且,一个查询只有一个
from
语句和一个
where
语句。所有
join
s都放在
from
语句中

两个好的实践是使用表别名,它们是表的缩写(您有时会这样做)。而且,不要将
连接
内部连接
混合使用。它们是同义词,但在查询中只能使用一个

SELECT u.user_id, u.username, u.profile, tt.tag_id, ta.tag_id, ta.tags,
       p.post_id, p.post_content, p.post_date, p.post_topic, p.post_by, p.invisipost 
FROM `posts` p join
     `users` u
      on p.post_by = u.user_id join
     `topic_tags` tt
      ON p.post_topic = tt.topic_id join
     `tags` ta
     ON tt.tag_id = ta.tag_id
WHERE p.post_topic='$id';

最后,我很确定您不想要
和tt.tag\u id=topics.topic\u id
。这是比较
标记id
主题id
。他们指的不是同一件事。我认为上面显示的
join
s足以满足您的查询。

感谢您的回复,我得到了“警告:mysql\u fetch\u array()期望参数1是资源,第76行给出的布尔值”,即“$result2=mysql\u query($sql2);而($rows=mysql\u fetch\u array($result2)){”您是否可以尝试直接针对MySQL运行此语句(而不是通过PHP)?您可以使用PHPMyAdmin或MySQL Workbench等“未知列”topics.topic_id”在“where子句”中是的,topic_id在topics和topic_标记中,标记通过topic_标记表与主题相关联。感谢您的回复,我将标记_id加入topic_id,因为topic_id在topics中,标记在标记中,然后topic_标记是标记_id和topic_id连接的位置。我尝试了在您的建议下,我仍然收到“警告:mysql_fetch_array()期望参数1为资源”的消息。我认为您不需要查询中的主题表。您可以直接从
帖子
连接到
主题标签
(那里的
条件是错误的)