public function get_advertisement($userid){
$query="SELECT *
FROM
advertisements
WHERE
advertisements.adtype = 'website'
HAVING
advertisements.id NOT IN (SELECT
work_done_ads.ad_id
FROM
work_done_ads
WHERE
work_done_ads.user_id = '".$userid."')
order by advertisements.id asc
LIMIT 1";
$result= $this->db->query($query)->row();
$query = $this->db->query("SELECT *
FROM
advertisements
WHERE
advertisements.adtype = 'website'
HAVING
advertisements.id NOT IN (SELECT
work_done_ads.ad_id
FROM
work_done_ads
WHERE
work_done_ads.user_id = '".$userid."')
order by advertisements.id asc
LIMIT 1");
if ($query->num_rows() > 0) {
foreach ($query->result_array() as $row)
{
return $result;
}
} else {
$query = $this->db->query("SELECT ad_cycle from users WHERE userid='$userid' LIMIT 1");
if ($query->num_rows() > 0) {
foreach ($query->result_array() as $row)
{
$ad_cycle = $row["ad_cycle"];
$ad_cycle = $ad_cycle + 1;
}
}
$this->db->query("UPDATE users SET ad_cycle=$ad_cycle WHERE userid='$userid' LIMIT 1");
$this->db->query("DELETE FROM work_done_ads WHERE user_id='$userid'");
$query="SELECT *
FROM
advertisements
WHERE
advertisements.adtype = 'website'
HAVING
advertisements.id NOT IN (SELECT
work_done_ads.ad_id
FROM
work_done_ads
WHERE
work_done_ads.user_id = '".$userid."')
order by advertisements.id asc
LIMIT 1";
$result= $this->db->query($query)->row();
return $result;
}
}
如果用户完成所有广告而不是视图计数器设置为0,则此查询会从用户尚未看到的数据库中获取广告,以便用户可以继续看到广告,这是一个持续的循环。
此查询使用太多资源,服务器提供508错误,有没有办法编写使用较少服务器资源的此查询