如何使用 float 添加InCondition?
我尝试了很多。
这工作正常:
$criteria=new CDbCriteria();
$criteria->addInCondition('order_id',array(36907));
$tasks=OrderTask::model()->findAll($criteria);
在我的例子中,它返回 4 个模型:
但如果我尝试
$criteria=new CDbCriteria();
$criteria->addInCondition('order_id',array(36907));
$criteria->addInCondition('step',array(3.20));
$tasks=OrderTask::model()->findAll($criteria);
或者
$criteria=new CDbCriteria();
$criteria->addInCondition('step',array("3.20"));
$tasks=OrderTask::model()->findAll($criteria);
或者
$criteria=new CDbCriteria();
$criteria->addInCondition('step',array('3.2'));
$tasks=OrderTask::model()->findAll($criteria);
结果为空。
根据log,查询为:
system.db.CDbCommand.query(SELECT * FROM
orders_tasks
t
WHERE step=:ycp1. Bound with :ycp1=3.2)
phpmyadmin 中的这个查询返回 5360 行
SELECT * FROM `orders_tasks` `t` WHERE step = 3.20
phpmyadmin 中的查询返回 0 行
SELECT * FROM `orders_tasks` `t` WHERE step = '3.20'
SELECT * FROM `orders_tasks` `t` WHERE step = '3.2'
SELECT * FROM `orders_tasks` `t` WHERE step = "3.20"
这次尝试
$criteria=new CDbCriteria();
$criteria->addInCondition('step',array("3,20"));
$tasks=OrderTask::model()->findAll($criteria);
返回 step=3 或 20 的模型
phpmyadmin 中的这个查询返回 step=3 OR 20 的行
SELECT * FROM `orders_tasks` `t` WHERE step = '3,20'
那么,如何使用 float addInCondition
?
细节,例如
step
字段为 float(8,2)
SQL 表转储:
CREATE TABLE IF NOT EXISTS `orders_tasks` (
`task_id` int(11) NOT NULL AUTO_INCREMENT,
`order_id` int(11) NOT NULL,
`step` float(6,2) NOT NULL,
`done` int(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`task_id`),
KEY `order_id` (`order_id`),
KEY `step` (`step`),
KEY `orderstep` (`order_id`,`step`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;
Yii 版本:1.1.10
最佳答案
DROP TABLE IF EXISTS `prefix_test`;
CREATE TABLE IF NOT EXISTS `prefix_test` (
`task_id` int(11) NOT NULL AUTO_INCREMENT,
`order_id` int(11) NOT NULL,
`step` float(6,2) NOT NULL,
`done` int(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`task_id`),
KEY `order_id` (`order_id`),
KEY `step` (`step`),
KEY `orderstep` (`order_id`,`step`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;
INSERT INTO `prefix_test` VALUES (1,36907,3.20,0);
INSERT INTO `prefix_test` VALUES (2,36907,3.21,0);
INSERT INTO `prefix_test` VALUES (3,37907,4.13,0);
$criteria=new CDbCriteria();
$criteria->addInCondition('order_id',array(36907));
$criteria->addInCondition('step',array(3.20));
$tests=Test::model()->findAll($criteria);
echo "Rows: ".count($tests)."<br>";
#Returns Rows: 0
在 Yii 日志中查询
SELECT * FROM `prefix_test` `t` WHERE (order_id=:ycp0) AND (step=:ycp1). Bound with :ycp0=36907, :ycp1=3.2
MySql 日志中的真实查询
SELECT * FROM `prefix_test` `t` WHERE (order_id=36907) AND (step='3.2')
这会解决你的问题
ALTER TABLE `prefix_test` CHANGE `step` `step` decimal(10,2) NOT NULL;
之后你的查询返回
#Returns Rows: 1
关于php - Yii addInCondition 与 float : How to ? 为什么 addInCondition ('column' , array(1.1, 1.3)) 不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14724551/