好的,下面是完整的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);
我怎样才能让它与多行一起工作?
2条答案
按热度按时间mv1qrgav1#
为什么不简单:
您的错误表明groupcomments.creator有多个group='$viewgroupid',出现错误是因为您不能在同一行的同一列中放置多个名称。使用连接时,不会发生这种情况(您可能会得到重复的行,但您的特定查询不会有这种问题)。
hth,设置
tyky79it2#
你可以重新设计你的sql代码,