php - Codeigniter 事件记录 where 子句包含来自两个字段的值

标签 php mysql database codeigniter

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

相关文章:

mysql - 如何构建数据库模式以允许 "1 in a million"情况?

php - 为什么不更新到数据库?

sql - 将 Visual Foxpro 数据库导入到 SQL Server

php - 在 linux mint 32 位操作系统中配置 xampp

php - 使用 jquery taggd 标记/标记图像

php - 如何使用多个Auth组件?

database - 实现数据库记录哈希以跟踪记录是否已更改

php - 根据 PHP 中的语言环境为 IntlDateFormatter()->format() 生成适当的模式

mysql - 如何使用 MySQL 连接获取最近 15 天未售出的产品。

mysql - cloud9-ide在mysql中导入数据库