![](/img/trans.png)
[英]How to get limited data parameter from joined table in codeigniter?
[英]Codeigniter do not select the data whose date is less than 2019 which have some types from joined table
我正在處理 Codeigniter rest API 項目並遇到了一個問題。 我希望我的模型不要選擇注冊日期小於 01/01/2019 並且連接表中類型等於 type_1 type_2 的數據,但是如果注冊日期小於 2019 並且連接表中類型等於 type_3 , type_4 請求必須返回記錄。
所以我嘗試了這個方法我怎么能得到這個結果?
code: // MyModel
function get_data() {
$id = isset($_REQUEST['id']) ? $_REQUEST['id'] : '';
if ($id != null) {
$this->db->where('ID',$id);
$result = $this->db->select('POST.ID,POST.DATE,'
. ' TYPE.Post_Type P_TYPE,)
->join('TYPE', 'POST.TYPE_ID = TYPE.ID', 'left')
->order_by('POST.ID', 'DESC')
->where_in('POST.Post_Type = type_1, type_2')
->where('POST.DATE<', "to_date('01/01/19', 'DD/MM/RR')", FALSE)
->get('POST');
}
return $result;
}
所以我希望它不返回 type_1 type_2 小於 2019 的數據,但可以獲取其他類型
首先,你為什么要過濾 where post.date 小於如果你想要大於那個日期?。 其次, where_in 確實接受值作為第二個參數的數組。 好吧,這是我的代碼,我將您的代碼重新組織為一個更易讀的代碼。 如果您嘗試在時間戳或日期字段中查詢日期,則它的格式應為 Ymd
function get_data() {
$id = isset($_REQUEST['id']) ? $_REQUEST['id'] : '';
$types = array('type_1', 'type_2');
if ($id != null) {
$this->db->where('ID',$id);
$this->db->select('POST.ID, POST.DATE, TYPE.Post_Type as P_TYPE,', FALSE);
$this->db->join('TYPE', 'POST.TYPE_ID = TYPE.ID', 'left');
$this->db->order_by('POST.ID', 'DESC');
$this->db->where_in('POST.Post_Type', $types);
$this->db->where('POST.DATE >', date('Y-m-d', strtotime('01-01-2019')) );
$this->db->from('POST');
$result = $this->db->get();
}
return $result;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.