mysql返回多个查询错误

tcbh2hod  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(307)

好的,下面是完整的php代码

$sql = "
 SELECT text 
      , creator
      , (SELECT name 
           FROM accounts 
          WHERE id IN (SELECT creator  
                         FROM groupcomments 
                        WHERE `group` = '$viewgroupid'
        ) AS account_name
      , (SELECT lastname 
           FROM accounts 
          WHERE id IN (SELECT creator 
                        FROM groupcomments 
                       WHERE `group` = '$viewgroupid'
                      )
        ) AS account_lastname
      , (SELECT role 
           FROM accounts 
          WHERE id IN (SELECT creator 
                         FROM groupcomments 
                        WHERE `group` = '$viewgroupid'
                      )
        ) AS account_role
      , (SELECT picture 
            FROM accounts 
           WHERE id IN (SELECT creator 
                          FROM groupcomments 
                         WHERE `group` = '$viewgroupid'
                       )
        ) AS account_picture
      , (SELECT id 
           FROM accounts 
          WHERE id IN (SELECT creator 
                         FROM groupcomments 
                        WHERE `group` = '$viewgroupid'
                      )
        ) AS account_id 
     FROM groupcomments 
    WHERE `group`='$viewgroupid'
 ";
$result = $conn->query($sql);

if ($result->num_rows > 0) {

    while($row = $result->fetch_assoc()) {
      $gc_text = $row['text'];
      $u_name = $row['account_name'];
      $u_lastname = $row['account_lastname'];
      $u_userid = $row['account_id'];
      $u_picture = $row['account_picture'];
      $u_role = $row['account_role'];
      include 'files/social/groupcomment.php';
    }
 //
}

基本上,它应该做的是从表中选择数据 accounts 从table上 groupcomments 然后包含一个文件,该文件将回显名称和姓氏,当表中只有一行时,这种方法非常有效 groupcomments 但是,当我添加另一行时,会出现以下错误: Subquery returns more than 1 row in htdocs/group.php:134 Stack trace: #0 htdocs/group.php(134): mysqli->query('SELECT text, cr...') #1 {main} thrown in htdocs/group.php on line 134 第134行是: $result = $conn->query($sql); 我怎样才能让它与多行一起工作?

mv1qrgav

mv1qrgav1#

为什么不简单:

$sql = "
     SELECT gc.text 
          , gc.creator
          , a.name     AS account_name
          , a.lastname AS account_lastname
          , a.role     AS account_role
          , a.picture  AS account_picture
          , a.id       AS account_id 
         FROM groupcomments gc
         JOIN accounts a on gc.creator = a.id
        WHERE gc.`group`='$viewgroupid'
     ";

您的错误表明groupcomments.creator有多个group='$viewgroupid',出现错误是因为您不能在同一行的同一列中放置多个名称。使用连接时,不会发生这种情况(您可能会得到重复的行,但您的特定查询不会有这种问题)。
hth,设置

tyky79it

tyky79it2#

你可以重新设计你的sql代码,

$sql="SELECT 
text , creator, `ac`.name as account_name, `ac`.lastname as account_lastname, `ac`.role as account_role, `ac`.`id` as account_id 

FROM groupcomments AS gp 
INNER JOIN accounts as ac
ON `ac`.`id` =`gp`.`creator`

WHERE 
`gp`.`group`='$viewgroupid' 

"; 
$result = $conn->query($sql);

if ($result->num_rows > 0) {

while($row = $result->fetch_assoc()) {
  $gc_text = $row['text'];
  $u_name = $row['account_name'];
  $u_lastname = $row['account_lastname'];
  $u_userid = $row['account_id'];
  $u_picture = $row['account_picture'];
  $u_role = $row['account_role'];
  include 'files/social/groupcomment.php';
}

}

相关问题