简体   繁体   English

子查询返回超过1行错误

[英]Subquery returns more than 1 row error

I have two queries that I want to run. 我有两个要运行的查询。 The result should be that the rows of dilemmas/questions are listed with the answers below the questions. 结果应该是难题/问题的行在问题下方列出了答案。 The answers are hidden though by javascript until I choose to press the image-button, which should then show the answer that belongs to the chosen question. 答案是用javascript隐藏的,直到我选择按下图像按钮,然后该按钮应显示属于所选问题的答案。

The code: 编码:

$result = mysqli_query($mysqli,"SELECT rid, pid, qid, aid, points FROM result WHERE rid=$val");
$answertext = mysqli_query($mysqli, "SELECT answer FROM answer_det WHERE pid=(SELECT pid FROM result WHERE rid=$val) AND qid=(SELECT qid FROM result WHERE rid=$val) AND aid=(SELECT aid FROM result WHERE rid=$val)");

while($row = mysqli_fetch_array($result) AND $row2 = mysqli_fetch_array($answertext))
{
    $resultp = $row['points'];
    $color = "#000000";

if (($resultp >= 1) && ($resultp <= 3))
   $color = "#FF0000";
else if (($resultp >= 3) && ($resultp <= 6))
   $color = "#FF9900";
else if (($resultp >= 6) && ($resultp <= 10))
   $color = "#07d407";

    echo "<div class='question-wrap'>
    <b><small>Dilemma ".$row['qid']." - Answer ". $row['aid'].": </small><span style=\"color: $color\">". $resultp."</span></b> of <b>10  <small>Points</small></b>
    <input type='image' class='answer-toggle' title='Information' src='img/down.png' width='13' height='10'>
    <p class='answer'>". $row2['answertext']."</p></div>";  }

I can't figure out what is wrong. 我不知道怎么了。 This is the message I get: 这是我收到的消息:

Warning: mysqli_query(): (21000/1242): Subquery returns more than 1 row in D:\home\site\wwwroot\devlopment\respondent2.php on line 122 Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in D:\home\site\wwwroot\devlopment\respondent2.php on line 125

This is line 122: 这是第122行:

$answertext = mysqli_query($mysqli, "SELECT answer FROM answer_det WHERE pid=(SELECT pid FROM result WHERE rid=$val) AND qid=(SELECT qid FROM result WHERE rid=$val) AND aid=(SELECT aid FROM result WHERE rid=$val)");

This is line 125: 这是第125行:

while($row = mysqli_fetch_array($result) AND $row2 = mysqli_fetch_array($answertext))

When you use a query like 当您使用类似查询

WHERE your_column = (SELECT ... WHERE ...)

The subselect must return only one row; 子选择必须只返回一行; if it does not, then you get the error you are seeing. 如果不是,那么您将看到错误。

A quick solution could be to change it to 一个快速的解决方案是将其更改为

WHERE your_column = (SELECT ... WHERE ... LIMIT 1)

but I'd use joins instead: 但我会改用连接:

SELECT answer
FROM answer_det
JOIN result USING (pid, qid, aid)
WHERE result.rid = $val

Remove the subqueries and do a left join on the table result or do this (you will only take the first line that appears): 删除子查询并在表结果上进行左连接或执行此操作(您将只选择出现的第一行):

$answertext = mysqli_query($mysqli, "SELECT answer FROM answer_det WHERE pid=(SELECT top 1 pid FROM result WHERE rid=$val) AND qid=(SELECT top 1 qid FROM result WHERE rid=$val) AND aid=(SELECT top 1 aid FROM result WHERE rid=$val)");

Your subqueries can't return more than one value to compare or anything else. 您的子查询最多只能返回一个要比较的值或其他任何值。

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

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