How To Set Conditions In Cdbcriteria Based On Two Tables Data

hi everyone.

I have two tables. one is "ad" and other is "ad_settings".

in "ad" tables I am creating new ads.

in "ad_settings" I have set the no_of_ad per page.

Now on the index page I want to show the advertizments based on the "ad_settings" table field "no_of_ad".

I am using cdbcriteria and my code is


<?php

$criteria = new CDbCriteria();

$criteria->addCondition('end_date > '.$now);

$criteria->order = 'RAND()';


$sideadvertisement = Ad::model()->findAll($criteria);

foreach ($sideadvertisement as $data):

    ?>

    <a href="<?php echo $data->ad_url; ?>" target="_blank"><?php echo CHtml::image(yii::app()->baseUrl . '/media/ad/thumbnails/' . $data->ad_image, $data->title, array('class' => 'img-responsive side-add')); ?></a>


    <?php

endforeach;

?>

how can I do this ?

should I create a relationship between them or using join ?

if join then how ??

thanks in advance.