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

How to print multiple rows using sub query?

我是 php 的新人。我有一个代码,我尝试使用子查询

打印多行
<?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   
}
?> 

此代码在我的 php 文件中显示空白结果。但是当我在 PhpMyadmin 中 运行 这个查询时,它显示错误,它包含 more than one line

如果我用 limit

更改我的子查询

喜欢

SELECT opinion_id FROM pacra_client_opinion_relations WHERE client_id = 50 LIMIT 1

然后打印记录

但是id =50包含4条记录。

I want to print all record 意思是想打印多行。我该怎么做?

r.client_id = (your sub query)

执行此操作时,查询试图将 ID 与子查询中的 一个 数据匹配。但是,正如您所提到的,此查询 returns 四个数据。所以你要检查ID是否在子查询数据中,不等于它。

r.client_id IN (your sub query)

添加 LIMIT 1 时查询有效的原因是因为您的子查询只返回了一条数据,正如预期的那样。

在此处查看有关 IN 子句的更多信息:

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