简体   繁体   English

SQL-选择ID,其中ID不在数组中

[英]SQL - select id where id is not in array

I want to select after 4 news the last 12 from a table. 我想在4条新闻之后从表中选择最后12条。 I tried but no results.I got the Undefined $news 我尝试了但没有结果,我得到了未定义的$ 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;
}

Help me please.How can I exclude the first 4 id 请帮助我。如何排除前4个ID

Try this: 尝试这个:

 ...
$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");
 ...

Just use the limit clause 只需使用limit子句

... ...

$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");

... ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM