douhuangjian9627 2015-09-07 10:12
浏览 45
已采纳

如何使用子查询打印多行?

I am new in php. I have a code in which i try to print multiple rows using sub query

<?php
include ("connection.php");

$q_opinion="SELECT r.client_id,c.id,t.id,a.id,o.id,c.name as opinion, r.notification_date, t.title as ttitle,a.title as atitle,o.title as otitle, l.title as ltitle, s.title as stitle, pr.opinion_id, pc.id, pr.client_id as pr_client, pc.address, pc.liaison_one, city.id, pc.head_office_id, city.city, pc.title as cname
FROM og_ratings r 
LEFT JOIN og_companies c
ON r.client_id = c.id
LEFT JOIN og_rating_types t
ON r.rating_type_id = t.id
LEFT JOIN og_actions a
ON r.pacra_action = a.id
LEFT JOIN og_outlooks o
ON r.pacra_outlook = o.id
LEFT JOIN og_lterms l
ON r.pacra_lterm = l.id
LEFT JOIN og_sterms s
ON r.pacra_sterm = s.id
LEFT JOIN pacra_client_opinion_relations pr
ON pr.opinion_id = c.id
LEFT JOIN pacra_clients pc
ON pc.id = pr.client_id
LEFT JOIN city
ON city.id = pc.head_office_id
WHERE r.client_id  = (SELECT opinion_id FROM pacra_client_opinion_relations WHERE client_id = 50)
";
$result = mysql_query($q_opinion) or die;
$rating = array();
while($row = mysql_fetch_assoc($result))
{
  $rating[] = $row['cname'];
  $action[] = $row['atitle'];
  $opinion[] = $row['opinion'];
}
for ($i=0; $i<count($rating); $i++)
{ ?>
    <table border="1">
    <tr>
         <td><?= $rating[$i] ?> </td>
         <td><?= $action[$i] ?> </td>
         <td><?= $opinion[$i] ?> </td>

    </tr>
    </table>
<?php   
}
?> 

This code show me blank result in my php file. But when i run this query in PhpMyadmin it show me error that it contain more than one line

if i change my subquery with limit

like

SELECT opinion_id FROM pacra_client_opinion_relations WHERE client_id = 50 LIMIT 1

Then it print record

But id =50 contain 4 records.

I want to print all record mean want to print multiple rows. How i can do it?

  • 写回答

1条回答 默认 最新

  • duanqianpu6499 2015-09-07 10:24
    关注
    r.client_id = (your sub query)
    

    When doing this, the query is trying to match the ID with one piece of data from the sub query. However, as you've mentioned, this query returns four pieces of data. So you want to check if the ID is IN the subquery data, not equal to it.

    r.client_id IN (your sub query)
    

    The reason your query worked when you added LIMIT 1 is because your sub query was only returning one piece of data, as expected.

    Take a look here for more information on the IN clause:

    http://www.tutorialspoint.com/mysql/mysql-in-clause.htm

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改