Php 如何连接三个表并添加一个;及;操作人员
作为一名菜鸟,我有点不知道如何做到这一点。我正试着加入三张桌子。这是表结构:Php 如何连接三个表并添加一个;及;操作人员,php,join,distinct,multiple-tables,Php,Join,Distinct,Multiple Tables,作为一名菜鸟,我有点不知道如何做到这一点。我正试着加入三张桌子。这是表结构: Accounts Table: 1) id 2) User_id (Id given to user upon sign up) 3) Full_name 4) Username 5) Email 6) Password Contacts Table: 1) My_id (This is the user who added friend_id) 2) Contact_id (this is the con
Accounts Table:
1) id
2) User_id (Id given to user upon sign up)
3) Full_name
4) Username
5) Email
6) Password
Contacts Table:
1) My_id (This is the user who added friend_id)
2) Contact_id (this is the contact who was added by my_id)
3) Status (status of relationship)
Posts Table:
1) Post_id
2) User_posting (this is the user who posted it)
3) Post_name
4) User_allowed (this is where its specified who can see it)
以下是我的代码结构:
<?php
$everybody = "everybody";
$sid = "user who is logged in.";
$sql = <<<SQL
SELECT DISTINCT contacts.contact_id, accounts.full_name, posts.post_id, posts.post_name
FROM contacts, accounts, posts
WHERE
(contacts.my_id = '$sid'
AND contacts.contact_id = accounts.user_id)
OR (contacts.my_id = '$sid'
AND contacts.contact_id = accounts.user_id)
OR (posts.user_posting = '$sid'
AND contacts.contact_id = accounts.user_id
AND posts.user_allowed = '$everybody')
OR (posts.user_id = '$sid'
AND contacts.user_id = accounts.user_id
AND posts.user_allowed = '$everybody')
LIMIT $startrow, 20;
$query = mysql_query($sql) or die ("Error: ".mysql_error());
$result = mysql_query($sql);
if ($result == "")
{
echo "";
}
echo "";
$rows = mysql_num_rows($result);
if($rows == 0)
{
print("");
}
elseif($rows > 0)
{
while($row = mysql_fetch_array($query))
{
$contactid = htmlspecialchars($row['contact_id']);
$name = htmlspecialchars($row['full_name']);
$postid = htmlspecialchars($row['post_id']);
$postname = htmlspecialchars($row['post_name']);
// If $dob is empty
if (empty($contactid)) {
$contactid = "You haven't added any friends yet.";
}
print("$contactid <br>$postname by $name <br />");
}
}
应该是这样的(未彻底检查):
假设您只想显示朋友的帖子(而不是自己的):
编辑:解释查询
查询SELECT
s用于输出的特定字段
FROM
posts
表,因为结果应该包含每个post的一行
与帐户表联接以获取过帐用户和用户的数据
JOIN
ed与contacts
表一起使用,这样我们就可以筛选出只有发帖用户也是相关用户添加的朋友$startrow
定义的偏移量您应该使用正确的
JOIN
语法。您期望的输出是什么?您期望的输出是什么?您的输出有什么问题?你期望什么?为什么要检查相同的条件两次(contacts.my_id='$sid'和contacts.contact_id=accounts.user_id)或(contacts.my_id='$sid'和contacts.contact_id=accounts.user_id)
由于注释被锁定,我建议您去阅读更多关于PHP和MySQL的内容。你们似乎对这两个方面都缺乏一些基本的了解。嘿,我试过你们的,结果显示错误:未知列“accounts.id”在“on子句”中“accounts表上难道并没有id字段吗?”?你能提供你的数据库结构吗?是的我能。。。但我无法理解为什么它没有发现它。数据库结构如我在上面发布的一样。请添加mysql表定义。好的,滚动调试会话现在停止。我如何查看前面的注释?我需要他们帮我查询。
SELECT DISTINCT contacts.contact_id,
accounts.full_name,
posts.post_id,
posts.post_name
FROM contacts
INNER JOIN accounts ON accounts.id = contacts.id
INNER JOIN posts ON posts.User_posting = accounts.id /* (where'd get posts.user_id btw ) */
WHERE contacts.my_id = $sid
AND posts.user_allowed = $everybody
LIMIT $startrow, 20
$everybody = "everybody";
$sid = user who is logged in.
$sql = "SELECT a.User_id, a.Full_name, p.Post_id, p.Post_name
FROM posts p
INNER JOIN accounts a ON a.User_id = p.User_posting
INNER JOIN contacts c ON c.My_id = '$sid' AND c.Contact_id = a.User_id
WHERE p.User_allowed = '$everybody'
LIMIT $startrow, 20";