繁体   English   中英

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

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

在尝试在codeigniter中实现简单的where子句时遇到困难。 我有这个

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();
        }

我需要从minQty列中选取值“ 10”,但没有办法实现。 这样,一旦我更改了数据库中的minQty,就自动获得正确的低数量。 请查看我的表格结构。

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);

尝试这个:

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

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

正如您提到的,minqty在代码中是动态的

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

您可以通过两种方式执行此操作。

1号

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

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();

注意:您使用productQty但你的数据库列名productqty 。它可以work.But使用正确的。

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

暂无
暂无

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

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