笨:
$this->db->select("item.*");
$this->db->from("items");
$this->db->where_not_in('item.ID', $Not);
$this->db->where_in('item_sub_category.Sub_category', $Sub_category);
$this->db->or_where_in('item_category.Category',$Category, NULL, FALSE);
$this->db->limit(4);
return $this->db->get()->result_array();
输出:
SELECT `item`.*
FROM `item`
LEFT JOIN `item_category`
ON `item_category`.`item` = `item`.`id`
LEFT JOIN `item_sub_category`
ON `item_sub_category`.`item` = `item`.`id`
WHERE `item`.`id` NOT IN( '12' )
AND `item_sub_category`.`sub_category` IN( '65', '66', '67', '68' )
OR `item_category`.`category` IN( '35', '36' )
LIMIT 4
预期产出:
SELECT `item`.*
FROM `item`
LEFT JOIN `item_category`
ON `item_category`.`item` = `item`.`id`
LEFT JOIN `item_sub_category`
ON `item_sub_category`.`item` = `item`.`id`
WHERE `item`.`id` NOT IN( '12' )
AND (`item_sub_category`.`sub_category` IN( '65', '66', '67', '68' )
OR `item_category`.`category` IN( '35', '36' ))
LIMIT 4
任何人都可以告诉我如何使用有效记录实现我的预期输出?
答案 0 :(得分:0)
您只需在活动记录中添加join
即可。如下。
$this->db->select("item.*");
$this->db->from("items");
$this->db->join("item_category","`item_category`.`item` = `item`.`id`","left");
$this->db->join("item_sub_category","`item_sub_category`.`item` = `item`.`id`","left");
$this->db->where_not_in('item.ID', $Not);
$this->db->where_in('item_sub_category.Sub_category', $Sub_category);
$this->db->or_where_in('item_category.Category',$Category, NULL, FALSE);
$this->db->limit(4);
return $this->db->get()->result_array();
答案 1 :(得分:0)
//试试这个..
$array = array('item_sub_category.Sub_category' => $Sub_category, 'item_category.Category' => $Category);
$this->db->select("item.*");
$this->db->from("items");
$this->db->join("item_category","`item_category`.`item` = `item`.`id`","left");
$this->db->join("item_sub_category","`item_sub_category`.`item` = `item`.`id`","left");
$this->db->where_not_in('item.ID', $Not);
$this->db->where_in($array);
$this->db->limit(4);
return $this->db->get()->result_array();
答案 2 :(得分:0)
你没有提出整个查询 - 但如果它涉及你所处的条件 - 你可以做以下事情:
$this->db
->select("item.*")
->from("item")
->where_not_in('item.ID', $Not)
->group_start()
->where_in('item_sub_category.Sub_category', $Sub_category)
->or_where_in('item_category.Category',$Category, NULL, FALSE)
->group_end()
->limit(4);
echo $this->db->get_compiled_select();