簡體   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