0

我的查询:

$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')

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

4

3 回答 3

2

如果您浏览codeigniter用户指南.. 您会看到有 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();

这很好......

于 2012-12-26T06:34:34.850 回答
1

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

$query = "your query";

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

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

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

于 2012-12-26T06:22:00.707 回答
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 =  ?)
于 2013-03-21T23:19:45.127 回答