我一直试图在 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);
最佳答案
您可以通过两种方式执行此操作。
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
。它可能有效。但请使用正确的列。
关于php - Codeigniter 事件记录 where 子句包含来自两个字段的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31437528/