Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/257.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.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
Php JOIN sql语句出现问题_Php_Mysql_Sql - Fatal编程技术网

Php JOIN sql语句出现问题

Php JOIN sql语句出现问题,php,mysql,sql,Php,Mysql,Sql,我有两个数据库表,我正试图加入home\u comments和profile\u img。我尝试的是将home_comments数据库中的用户id与profile_img数据库进行匹配,以便从发表评论的人那里获取profile图像。我编写的查询没有中断,因为它正在生成结果,但是,查询的结果显示每个用户id在profile\u img数据库中的所有图像 每当有人上传一个概要文件时,我只是将其添加到数据库中,然后简单地调用其中的最后一个来显示概要文件。因此,我期待着同样的概念得到实施。要显示的用户最

我有两个数据库表,我正试图加入home\u comments和profile\u img。我尝试的是将home_comments数据库中的用户id与profile_img数据库进行匹配,以便从发表评论的人那里获取profile图像。我编写的查询没有中断,因为它正在生成结果,但是,查询的结果显示每个用户id在profile\u img数据库中的所有图像

每当有人上传一个概要文件时,我只是将其添加到数据库中,然后简单地调用其中的最后一个来显示概要文件。因此,我期待着同样的概念得到实施。要显示的用户最后一个配置文件\u img

每当我在查询末尾添加LIMIT 1时,它只允许显示一条注释,即total。我只需要为每个用户显示最后一个DESC profile\u img,但仍然显示所有注释。我怎样才能做到这一点

最初我有以下疑问:

$select_comments_sql = "
    SELECT * 
    FROM home_comments
    ORDER BY id DESC
";
我的查询尝试

$select_comments_sql = "
    SELECT c. *,
    p.user_id, p.img
    FROM home_comments AS c
    INNER JOIN profile_img AS p
    WHERE c.user_id = p.user_id
    ORDER BY id DESC
";
数据库设置:

主页评论

id, user_id, username, comment, date
配置文件\u img结构:

id, user_id, img
编辑:
我还注意到,它在数据库中为每个不同的照片显示相同的注释,因此重复了注释。不确定这是否与图像问题有关

带查询的SQL:

$select_comments_sql = "
    SELECT c.*, p.user_id, p.img
    FROM home_comments AS c
    INNER JOIN (SELECT max(ID) as ID, user_ID  
                FROM profile_img 
                GROUP BY user_ID) PI
      on PI.User_ID = C.USer_ID
    INNER JOIN   profile_Img p
      on PI.user_ID = P.User_ID 
     and PI.ID = P.ID
    ORDER BY C.id DESC
";
  if ($select_comments_stmt = $con->prepare($select_comments_sql)) {
        //$select_comments_stmt->bind_param("s", $user_id);
        $select_comments_stmt->execute();
        if (!$select_comments_stmt->errno) {
            //echo "error";
        }
        $select_comments_stmt->bind_result($comment_id, $comment_user_id, $comment_username, $home_comments, $comment_date, /*$commenter_id,*/ $commenter_user_id, $commenter_img);

        $comment_array = array();
        while ($select_comments_stmt->fetch()) {
            $comment_array[] = $comment_user_id;
            $comment_array[] = $comment_username;
            $comment_array[] = $home_comments;
            $comment_array[] = $comment_date;
            /*$comment_array[] = $commenter_id;*/
            $comment_array[] = $commenter_user_id;
            $comment_array[] = $commenter_img;
            $commenter_img = '<img class="home-profile-pic" src=" '.$commenter_img.'">';
            if ($home_comments === NULL) {
                echo 'No comments found.';
            } else {
                echo $commenter_img;
                echo $comment_username. "<br>";
                echo $home_comments. "<br><br><br>";
            }

一种方法是使用内联视图生成一组数据,其中包括每个用户照片的最大ID,然后使用此集合限制所有用户的照片

在下面的示例中,我生成了一个名为PI的数据集,其中包含每个用户的每个配置文件图像的最大ID。然后将其连接回整个集合,作为限制;因此,只返回用户的最新照片。 这在本质上确保了评论和图片之间有一个1-M的关系。多对多是造成问题的原因

SELECT c.*, p.user_id, p.img
FROM home_comments AS c
INNER JOIN (SELECT max(ID) as ID, user_ID  
            FROM profile_img 
            GROUP BY user_ID) PI
  on PI.User_ID = C.USer_ID
INNER JOIN   profile_Img p
  on PI.user_ID = P.User_ID 
 and PI.ID = P.ID
ORDER BY C.id DESC

如果您只为一个或几个用户运行查询,最快的方法可能是在select中使用子select,而不是使用联接,因为您没有严格地“联接”数据

SELECT c. *,
(
    SELECT p.img 
    FROM profile_img AS p
    WHERE p.user_id = c.user_id
    ORDER BY p.id DESC
    LIMIT 1
) AS img
FROM home_comments AS c
WHERE c.user_id = x

这将避免在只获取一个或几个时计算每个用户的最新配置文件图像,因为嵌套查询将无法加入索引。

我认为这应该可行,但我没有测试它

SELECT c.*, p.user_id, p.img, MAX(p.id) /* not sure if you need MAX(p.id) but try it with/without it here */
FROM home_comments c
LEFT OUTER JOIN profile_img p on p.user_id = c.user_id
WHERE p.id = MAX(p.id)
ORDER BY c.id DESC

我们可以假设最高配置文件_img.ID是您想要返回的最新图像吗?是的,这是正确的。我还注意到它为数据库中的每个不同照片显示相同的注释,因此重复了注释。不确定这是否与映像问题有关。您可以使用select maxid,user\U id from profile\U img group by user以中间方式加入表_id@becky同样的评论是因为M-M关系。你需要一个1-M,这样评论就不会重复。这似乎破坏了查询,但我无法确定在哪里。我不需要从profile\u img数据库中选择id吗?这将决定最新的图像。我只是将id添加到p.user_id,p.img中,但似乎没有帮助。我不明白为什么会出现这种情况。子查询返回ID、user\u ID并连接到user\u ID上的注释。第二个连接在profile\u Img上,并连接到user\u ID和profile\u Img.ID上的子查询。你犯了什么错误?或者,您可以在问题中发布一个示例表/数据,并将链接发回,以便我们找出问题所在?中断查询的描述不够详细。PHP是返回错误还是mySQL返回错误?如果是这样,有什么错误?当然,一分钟后我会发送。不幸的是,这破坏了查询。您是否逐字复制了它?我显然不能测试它,但是where子句只是一个例子。@CANCINAI,但是内联select不是每次都必须对返回的每个记录执行吗?在一个小数据集上,我可以看到这是有效的,但在一个更大的数据集上,我看到这需要更长的时间。我想我需要花一些时间准确地测试它,这就是为什么我只在一个小数据集上使用这个方法。我用“小”作为一个相对术语——如果用户总数只有100人,那么差别就可以忽略不计了。@Becky这个查询是用你的小提琴处理的-