好的,下面是完整的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
和表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);
如何使它与多行一起使用?
答案 0 :(得分:1)
为什么不简单:
$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'
";
您的错误表明,具有group ='$ viewgroupid'的groupcomments.creator不止一个,并且您收到此错误,因为您不能在同一行中放置多个名称。有了联接,就不会发生这种情况(您可能会得到重复的行,但是您的特定查询将不会出现该问题)。
HTH, 设置
答案 1 :(得分:0)
您可以重新设计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';
}
}