我能够解决这个问题但是,当我回应我的client_id时,有很多重复,我试图把array_unique和distinct分开但没有用,我还能做什么呢?我还希望它显示所有地址,因为有些客户端有多个地址。
$query1= "Select distinct client_id from client_profile where client_status= 'f'";
$result1= pg_query($conn2, $query1);
$clientid = array();
while($row1 = pg_fetch_array($result1)){
$id=$row1['client_id'];
$clientid[]=$id;
$clientid = array_unique($clientid);
}
//$clientid=array_unique($clientid);
$clientid=implode(',',$clientid);
$query= "select * from vouchers where client_id IN ($clientid)";
$result = pg_query($conn,$query);
?>
<!DOCTYPE html>
<html>
<head>
<title>Inactive Clients</title>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet">
<link href = "http://fonts.googleapis.com/css?family=Roboto:400">
</head>
<body>
<table class="table table-sm">
<thead>
<tr>
<th>Client id</th>
<th>File Name</th>
</tr>
</thead>
<?php
while($row = pg_fetch_array($result))
{
?>
<tbody>
<tr>
<td><?php echo $row['client_id']; ?></td>
<td><?php echo $row['file_name']; ?></td>
</tr>
<?php }?> </tbody>
</table>
</body>
</html>
答案 0 :(得分:1)
尝试运行以下查询。还要检查列名是否正确。
select client_id,array_agg(address_Field_column) from vouchers where client_id IN (select distinct client_id from client_profile where client_status= 'f') group by client_id;
我不知道你正在使用哪个postgre sql版本。 在mysql中,有一个 group_concat 函数。 供您参考See this
答案 1 :(得分:0)
您可以使用GROUP BY
子句。在这种情况下,您的查询可以读取如下内容:
QUERY 1
<?php
$query1 = "SELECT client_id FROM `client_profile` AS CP ";
$query1 .= " WHERE CP.client_status='f' GROUP BY CP.client_id ";
QUERY 2
<?php
$query2 = "SELECT * FROM `vouchers` AS VC ";
$query2 .= " WHERE VC.client_id IN ($clientid) GROUP BY VC.client_id ";
答案 2 :(得分:0)
放入查询的开头“SELECT DISTINCT” 你可能需要不止一次。