using joins for pdf generation

hi all,

   I am trying to use 2 query's where the first query's result will be used in second querys where condition.

I am able to display them in a pdf file. But i am not understanding how to write the query.

Do i need to use joins?

Please help me

The code i wrote is





$pdf->Write($h=3, $txt, $link='', $fill=0, $align='C', $ln=true, $stretch=0, $firstline=false, $firstblock=false, $maxh=0);


 $sql = "SELECT DISTINCT a.Demand_flat_number,a.Demand_amount FROM tbl_charges_demand_details a WHERE a.Demand_paid_flag='n'";




 $html='<br><br><br><table><tr><th></th><th><b>Apartment Number </b></th><th><b>Name of the member</b></th><th><b>Amount Due</b></th></tr>';

 $model=ChargesDemandDetails::model()->findAllBySql($sql);

   $sqln = "SELECT a.owners_name FROM tbl_apartment a WHERE a.flat_number='".$sql['Demand_flat_number']."'";

  $modeln=Apartment::model()->findAllBySql($sqln);


if(!empty($model) and !empty($modeln)){

  foreach($model as $data){

  	foreach($modeln as $datan)

  	{

  $html.='<tr><td></td><td>'.$data['Demand_flat_number'].'</td><td>'.$data['Demand_amount'].'</td><td>'.$datan['owners_name'].'</td></tr>';

  }

  }

}

$html.='</table>';

 $pdf->writeHTML($html, true, false, false, false, '');



I think you’d be best advised to use Yii’s very own query builder, as you are only pulling a few fields. Using full-blown active records looks like unnecessary overhead.