[英]SQL - select id where id is not in array
我想在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個ID
嘗試這個:
...
$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");
...
只需使用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.