简体   繁体   English

Codeigniter活动记录where子句,其值来自两个字段

[英]Codeigniter active record where clause with values from two fields

Am stuck trying to implement simple where clause in codeigniter. 在尝试在codeigniter中实现简单的where子句时遇到困难。 What I have is this 我有这个

public function get_low_stock()
        {
            $this->db->select('*');
            $this->db->where('productQty <=' , '10'); //line where the problem is
            $this->db->from('products');
            $query = $this->db->get();
            return $query->result();
        }

I need the value '10' to be picked from the minQty colum, but have no way to implement this. 我需要从minQty列中选取值“ 10”,但没有办法实现。 Such that once I change the minQty in the database, automatically I get the correct low qty. 这样,一旦我更改了数据库中的minQty,就自动获得正确的低数量。 Please see my table structure. 请查看我的表格结构。

CREATE TABLE IF NOT EXISTS `products` (
`productid` int(11) NOT NULL,
  `productname` varchar(30) NOT NULL,
  `catid` int(11) NOT NULL,
  `productqty` int(11) NOT NULL,
  `buyprice` int(11) NOT NULL,
  `saleprice` int(11) NOT NULL,
  `minqty` int(11) NOT NULL,
  `maxqty` int(11) NOT NULL,
  `alertlevel` int(11) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=latin1;

--
-- Dumping data for table `products`
--

INSERT INTO `products` (`productid`, `productname`, `catid`, `productqty`, `buyprice`, `saleprice`, `minqty`, `maxqty`, `alertlevel`) VALUES
(1, '2.6 china touch', 1, 9, 2500, 5000, 10, 100, 15),
(4, '2.9 china touch', 1, 10, 2500, 5000, 10, 100, 15),
(5, '3.0 small cable', 1, 5, 2500, 5000, 10, 100, 15);

Try this: 尝试这个:

$where = '(productQty <= minqty)';

$this->db->where($where);

as you mentioned minqty is dynamic in your code 正如您提到的,minqty在代码中是动态的

$this->db->where('productQty <= minqty');

You can do this two way. 您可以通过两种方式执行此操作。

1# 1号

$query=$this->db->query('SELECT * FROM `products` where productqty <= minqty');
return $query->result();

2# 2号

$this->db->select('*');        
$this->db->where('productqty <=minqty',null,false);
//or use this way
//$this->db->where('productqty <=minqty');
$this->db->from('products');
$query = $this->db->get();
return $query->result();

Note You used productQty but your database column name is productqty .It may work.But use the right one. 注意:您使用productQty但你的数据库列名productqty 。它可以work.But使用正确的。

如果同时查看两个答案,您会发现您的答案完全相同,只是略有不同

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM