1

I was able to fix the issue however, when i echo out my client_id there is a lot of repetition, and I tried to put array_unique and distinct but nothing worked, what else is there I could do? I also want it to show all the addresses because some clients have more than one address.

 $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>
jasmine825
  • 144
  • 2
  • 15
  • On which query is the repetition occurring? I think it's the second query, where you check for vouchers, not the first one. First one should work fine. You should also use UNIQUE on a table where you want the value to be unique (in your case, client_id should be unique in client_profile, then you wouldn't need to use DISTINCT there). – Janno Jul 29 '16 at 11:10

3 Answers3

1

Try running the below query. Also check the column names are correct or not.

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;

I don't know which postgre sql version you are using. In mysql, there is a group_concat function. For your reference See this

Community
  • 1
  • 1
Nithyanandhan M
  • 1,524
  • 1
  • 16
  • 26
0

You could use theGROUP BY clause for that. In this case your Query could read something like this:

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 ";
Poiz
  • 7,611
  • 2
  • 15
  • 17
0

Put in the beginning of your query "SELECT DISTINCT" you may need to put it more than once.