简体   繁体   English

如何连接三个表并添加“AND”运算符?

[英]How to join three tables and add a “AND” operator?

Being a Rookie i am a bit lost as to how to nail this. 作为一个新秀我有点迷失如何钉这个。 I am trying to join three tables. 我想加入三张桌子。 This is the table structure: 这是表结构:

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)

Here is the code structure i have: 这是我的代码结构:

<?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 />");
}

}

The problem is that the query shows me my posts plus all posts from friends. 问题是查询显示我的帖子以及朋友的所有帖子。

What am i doing wrong? 我究竟做错了什么?

Should be like this (Not thoroughly checked): 应该是这样(没有彻底检查):

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

Assuming you only want to show your friends' posts (and not your own): 假设您只想显示朋友的帖子(而不是您自己的帖子):

$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";

EDIT: Explaining the query. 编辑:解释查询。

  1. The query SELECT s certain fields used for output 查询SELECT用于输出的某些字段
  2. FROM the posts table since the result should contain a row for each post. FROM posts表中,因为结果应该包含每个帖子的一行。
  3. That base table is JOIN ed with accounts table to get the data of the posting user and 该基表与accounts表一起JOIN以获取发布用户的数据
  4. additionally JOIN ed with contacts table so we can filter to only have posting users that are also friends added by the user in question. 另外JOIN contacts表格,这样我们就可以过滤到只有相关用户添加的朋友的帖子用户。
  5. An additional filter is on the allowed value and 额外的过滤器在允许的值和
  6. for pagination purposes we have an offset defined by $startrow . 为了分页,我们有一个由$startrow定义的偏移量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM