[英]LEFT JOIN is not working for 2 tables
Basically I have 2 tables 基本上我有2张桌子
I am trying to use a left join so that I can link the "posted_by" in "topics" with "user_id" in "users", so that I can output both the users.username for display, as well as users.profile(avatar picture). 我试图使用左联接,以便可以将“主题”中的“ posted_by”与“用户”中的“ user_id”链接起来,以便既可以输出用于显示的users.username,也可以输出users.profile(头像图片)。
Here is my current code, which is giving me boolean errors. 这是我当前的代码,给我布尔错误。
<?php
include 'core/init.php';
include 'includes/overall/header.php';
$sql = " SELECT *, users.id, users.username, users.profile
FROM `topics`
LEFT JOIN
users ON topics.posted_by = " . mysql_real_escape_string($_GET['topic_id']) . " users.user_id ORDER BY `posted` DESC";
$result = mysql_query($sql);
// Start looping table row
while($rows = mysql_fetch_array($result)){
?>
<table>
<tr>
<td rowspan="4"> Avatar code to go here<br>
<? echo $rows['username']; ?></td>
<td><? echo $rows['category']; ?> > <? echo $rows['sub_category']; ?> </td>
</tr>
<tr>
<td><? echo $rows['posted']; ?></td>
</tr>
<tr>
<td><? echo $rows['topic_data']; ?></td>
</tr>
<tr>
<td><a href="view_topic.php?id=<? echo $rows['topic_id']; ?>">Reply</a> (<? echo $rows['reply']; ?>) Replies</td>
</tr>
</table>
<?php
// Exit looping and close connection
}
mysql_close();
?>
ON topics.posted_by = " . mysql_real_escape_string($_GET['topic_id']) . " users.user_id
you know this will produce ON topics.posted_by = 1 users.user_id
for example which is invalid SQL syntax. ON topics.posted_by = " . mysql_real_escape_string($_GET['topic_id']) . " users.user_id
您知道这将产生ON topics.posted_by = 1 users.user_id
,例如,这是无效的SQL语法。 Use WHERE instead 使用WHERE代替
ON topics.posted_by = users.user_id WHERE topics.id = (topic_id_variable)
PS: Using mysql_
is highly not recommended. PS:强烈建议不要使用mysql_
。 You should change the API. 您应该更改API。
You need to provide how users
relates to topics
in your join clause. 您需要在join子句中提供users
与topics
。 You have provided a outside variable to your join clause. 您已为join子句提供了一个外部变量。 This doesn't allow the database engine to establish a relationship on how the two tables should be joined. 这不允许数据库引擎建立如何连接两个表的关系。
I believe the query you are expecting should be something like this 我相信您期望的查询应该是这样的
$sql = " SELECT *, users.id, users.username, users.profile
FROM `topics`
LEFT JOIN
users ON topics.posted_by = users.user_id
WHERE topics.id = '" . mysql_real_escape_string($_GET['topic_id']) . "'
ORDER BY `posted` DESC";
I believe you are using something like 我相信您正在使用类似
$sql = "SELECT users.user_id, users.username, users.profile, topics.topic_id, topics.category, topics.sub_category,
topics.topic_data, topics.posted_by, topics.posted, topics.view, topics.reply
FROM users WHERE topics.posted_by = users.user_id ORDER BY topics.posted DESC";
Try adding 尝试添加
$sql = "SELECT users.user_id, users.username, users.profile, topics.topic_id, topics.category, topics.sub_category, topics.topic_data, topics.posted_by, topics.posted, topics.view, topics.reply FROM users, topics WHERE topics.posted_by = users.user_id ORDER BY topics.posted DESC"; $ sql =“选择users.user_id,users.username,users.profile,topics.topic_id,topics.category,topics.sub_category,topics.topic_data,topics.posted_by,topics.posted,topics.view,topics.reply来自用户,主题为 WHERE topic.posted_by = users.user_id ORDER BY topic.posted DESC”;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.