SQL - 选择id不在数组中的id

时间:2014-04-22 08:48:26

标签: php mysql sql codeigniter

我想从表格中选出4个新闻后的最后12个新闻。 我试过但没有结果。我得到了未定义的$ news

public function get_all_news_home()
{
    $news = array();
    # ne conectam la baza de date
    $this->load->database();
    # selectam stirile de top
    $last_news_query = $this->db->query("SELECT * FROM News WHERE Type = 1 AND Ready='Y' ORDER BY Date DESC LIMIT 4");
    $last_news = $this->db->query($last_news_query);
    $last_news = ($last_news->num_rows()) ? $last_news->result_array() : NULL;
    foreach ($last_news as $ln) 
    {
        array_push($news,array('id' => $ln['ID']));
    }

    $all_news = $this->db->query("SELECT * FROM News WHERE News.Ready = 'Y' AND News.ID NOT IN ('$news') ORDER BY Date DESC LIMIT 12");

    if($all_news->num_rows())
    {
        $all_news = $all_news->result_array();
    }
    else
    {
        $all_news = NULL;
    }
    return $all_news;
}

请帮帮我。如何排除前4个身份

2 个答案:

答案 0 :(得分:1)

试试这个:

 ...
$newsIds = implode(',', array_values($news));
 ...    
$all_news = $this->db->query("SELECT * FROM News WHERE News.ID NOT IN ('$newsIds') ORDER BY Date DESC LIMIT 12");
 ...

答案 1 :(得分:0)

只需使用限制条款

...

$last_news_query = $this->db->query("SELECT * FROM News WHERE Type = 1 AND Ready='Y' ORDER BY Date DESC LIMIT 0,4");

...

$all_news = $this->db->query("SELECT * FROM News WHERE News.Ready = 'Y' AND News.ID NOT IN ('$news') ORDER BY Date DESC LIMIT 4,12");

...