Codeigniter查询

时间:2012-12-26 06:15:35

标签: mysql codeigniter

我的查询:

$this->db->select('*');
$this->db->join('pos_item_sales', 'pos_item_sales.item_id = pos_item_infos.item_id');
$this->db->join('pos_batch_infos', 'pos_batch_infos.item_id = pos_item_infos.item_id');
$this->db->where("`pos_item_sales`.`transaction_id` =  '$transaction_id' AND (`pos_item_sales`.`item_barcode` =  '$term' OR `pos_batch_infos`.`item_mbarcode` =  '$term' OR `pos_item_infos`.`item_id` =  '$term')");
$query = $this->db->get('pos_item_infos');
echo $this->db->last_query();

它解决了我的问题,但我需要这样的查询:

$term = $this->input->get('term',TRUE);
$this->db->select('*');
$this->db->join('pos_item_sales', 'pos_item_sales.item_id = pos_item_infos.item_id');
$this->db->join('pos_batch_infos', 'pos_batch_infos.item_id = pos_item_infos.item_id');
$this->db->where('pos_item_sales.transaction_id',$transaction_id);
$this->db->where('pos_item_sales.item_barcode',$term);
$this->db->or_where('pos_batch_infos.item_mbarcode',$term);
$this->db->or_where('pos_item_infos.item_id',$term);
$query = $this->db->get('pos_item_infos');
echo $this->db->last_query();

但我需要一个像:

的查询
SELECT *
FROM (`pos_item_infos`)
JOIN `pos_item_sales` ON `pos_item_sales`.`item_id` = `pos_item_infos`.`item_id`
JOIN `pos_batch_infos` ON `pos_batch_infos`.`item_id` = `pos_item_infos`.`item_id`
WHERE `pos_item_sales`.`transaction_id` =  '11355822927'
AND (`pos_item_sales`.`item_barcode` =  '8801962686156'
OR `pos_batch_infos`.`item_mbarcode` =  '8801962686156'
OR `pos_item_infos`.`item_id` =  '8801962686156')

我如何解决这个问题请帮助,因为它不包括(或在我的条件中。)

3 个答案:

答案 0 :(得分:2)

如果您浏览codeigniter userguide ..您可以看到有4种方法可以调用where条款......

所有这些都做同样的事情......并且您的第一个代码是 codeigniter样式(如果您担心不是),这是codeigniter用户指南的第4种方法可以手动编写自己的子句 ...无论如何调用where函数没有区别......

所以我会使用你的第一个查询

$this->db->select('*');
$this->db->join('pos_item_sales', 'pos_item_sales.item_id = pos_item_infos.item_id');
$this->db->join('pos_batch_infos', 'pos_batch_infos.item_id = pos_item_infos.item_id');
$this->db->where("`pos_item_sales`.`transaction_id` =  '$transaction_id' AND (`pos_item_sales`.`item_barcode` =  '$term' OR `pos_batch_infos`.`item_mbarcode` =  '$term' OR `pos_item_infos`.`item_id` =  '$term')");
$query = $this->db->get('pos_item_infos');
echo $this->db->last_query();

这完全没问题......

答案 1 :(得分:1)

在复杂查询的情况下,我发现像这样发送原始查询更容易:

$query = "your query";

$result = $this->db->query($query);

在将变量插入查询之前不要忘记转义变量,如下所示:

$var = $this->db->escape($var);

答案 2 :(得分:0)

$query="SELECT *
FROM pos_item_infos
JOIN pos_item_sales ON pos_item_sales.item_id = pos_item_infos.item_id
JOIN pos_batch_infos ON pos_batch_infos.item_id = pos_item_infos.item_id
WHERE pos_item_sales.transaction_id =  ?
AND (pos_item_sales.item_barcode =  ?
OR pos_batch_infos.item_mbarcode =  ?
OR pos_item_infos.item_id =  ?)";
$params=array();
$params[]='11355822927';
$params[]='8801962686156';
$params[]='8801962686156';
$params[]='8801962686156';


$result=$this->db->query($query,$params);
$result=$result->result_array();
print_r($result);

另外,使用USING简化语法。

SELECT *
FROM pos_item_infos
JOIN pos_item_sales USING (item_id)
JOIN pos_batch_infos USING (item_id)
WHERE pos_item_sales.transaction_id =  ?
AND (pos_item_sales.item_barcode =  ?
OR pos_batch_infos.item_mbarcode =  ?
OR pos_item_infos.item_id =  ?)