【问题标题】:Codeigniter active record where clause with values from two fields带有来自两个字段的值的 Codeigniter 活动记录 where 子句
【发布时间】:2015-07-15 17:48:08
【问题描述】:

我一直试图在 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);

【问题讨论】:

    标签: php mysql database codeigniter


    【解决方案1】:

    这两种方法都可以。

    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。它可能有效。但使用正确的。

    【讨论】:

      【解决方案2】:

      试试这个:

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

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

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

      【讨论】:

      • 在我的情况下,我希望它是动态的,以便从 minqty 列中选择而不是每次都设置。即在数据库中,我可以将产品的 minqty 更改为 15。然后脚本会自动根据新的数据库值进行计算。
      • 如果我能理解您的查询,它将是这样 ===> select * from product where productty
      • 您选择了其他答案?
      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 2013-01-11
        • 1970-01-01
        • 1970-01-01
        • 2015-07-31
        • 1970-01-01
        • 2012-04-09
        • 2012-11-05
        • 2015-04-03
        • 1970-01-01
        相关资源
        最近更新 更多