简体   繁体   English

使用JOINING表创建mysql查询语法

[英]Creating a mysql query syntax with JOINING tables

I'm having trouble getting some data.... I was wondering if someone can help me, 我在获取一些数据时遇到了麻烦。...我想知道是否有人可以帮助我,

I have 4 tables (likes, follow, comment, users) 我有4个表格(点赞,关注,评论,用户)

I want to be able to populate my page when a user likes/comments/follows/etc... (if a user is following a particular user). 我希望能够在用户喜欢/评论/关注/等...(如果用户关注特定用户)时填充我的页面。

likes 喜欢

idlikes   idusers    iditem
  1          1          5
  2          2          4
  3          2          22

follow 跟随

idfollow   idusers_follower   idusers    idbusiness
  1              1               2           
  2              1               3           
  3              1               4           
  4              4               2           
  5              4                           1

comment 评论

idcomments   idusers    text
   1            1       asfd
   2            2       safd

users 用户

idusers
   1
   2
   3
   4

For example if I am id user #1, I'm following users #2, #3, #4 例如,如果我是用户#1,则我正在跟踪用户#2,#3,#4

My page would populate to show: 我的页面将显示为:

  • #2 likes item #4, #22. #2喜欢项目#4,#22。
  • #4 is following #2 (because I'm following #4, this is why its showing) #4正在关注#2(因为我正在关注#4,这就是其显示的原因)
  • #2 comments "safd" #2评论“ safd”

I'm not sure what is the best way to display this? 我不确定显示此内容的最佳方法是什么? I currently have multiple functions querying on table at a time, and I'm working on merging the arrays together? 我目前一次在表上查询多个函数,并且正在将数组合并在一起? Or should I use join tables? 还是应该使用联接表? Which I'm trying now... 我现在正在尝试...

Get users that I'm following. 吸引我关注的用户。

$feeds = new feed();
$meID = 1;
$query = "SELECT idusers FROM follow WHERE iduserse_follower = ?";

$users = $dbh -> prepare($query);
$users -> execute(array($meID));

while($following = $users -> fetch(PDO::FETCH_ASSOC)){
      $follow = $following['idusers']; //This will get all of useres I'm following
      $populate = $feeds->feed_all($follow); // from function
}

Query 询问

class feed()
{
     public function feed_all($idusers)
     {
         // SYNTAX HELP //////////////////////
           $query = "SELECT 
           f.idusers_follower, 
           f.idusers, 
           l.iditem, 
           c.text 
           FROM follow f, users u 
           JOIN likes l 
           ON l.idusers = f.idusers 
           JOIN comment c
           ON c.idusers = f.idusers
           WHERE f.idusers_follower = ? AND f.idusers_follower = l.idusers AND f.idusers_follower = c.idusers AND f.idusers = u.idusers"

           $pop = $dbh->prepare($query);
           $pop ->execute($idusers);

           // while loop to return value
    while($row = $pop -> fetch(PDO::FETCH_ASSOC))
    {
        $feed_data[]=$row;
    }
    return $feed_data;
     }
}

This is where I'm stuck. 这就是我卡住的地方。 :( I'm not even sure if I'm doing the statement right? :(我什至不确定我是否在做正确的陈述?

++++++++++ EDIT: ++++++++++++ ++++++++++编辑:++++++++++++

I have edited to add idbusiness 我已编辑以添加idbusiness

Now since I'm following #4, it would also show up that #4 is following #1. 现在,由于我正在关注#4,所以它也显示出#4正在关注#1。

Your current approach of performing three separate queries is as good as any; 您当前执行三个独立查询的方法与其他方法一样好。 you can combine them into a single resultset using UNION , which would be useful if you wanted to sort the combined results by some field (eg activity timestamp) and/or limit the combined results: 您可以使用UNION将它们合并为一个结果集,如果您想按某个字段(例如,活动时间戳记)对合并结果进行排序和/或限制合并结果,则将非常有用:

  SELECT idusers, 'likes' AS what, likes.iditem AS detail
  FROM   likes JOIN follow USING (idusers)
  WHERE  follow.idusers_follower = 1

UNION ALL

  SELECT f1.idusers, 'follows', f2.idusers
  FROM   follow f1 JOIN follow f2 ON f1.idusers = f2.idusers_follower
  WHERE  f1.idusers_follower = 1

UNION ALL

  SELECT idusers, 'commented', comment.text
  FROM   comment JOIN follow USING (idusers)
  WHERE  follow.idusers_follower = 1

See it on sqlfiddle . sqlfiddle上看到它。

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

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