doutu9810 2013-06-16 15:50 采纳率: 0%
浏览 53
已采纳

CDbCriteria - 模拟两个不等式条件 - Yii

I need to run a query that returns products that match age requirements given by a user. Products in the database are assigned a minimum and maximum age that apply (min_age & max_age). The user will select an age range they wish to search under (for the sake of this question let's call this the userMinAge & userMaxAge).

Since I want to return any product where there is some cross over between the user's selected age range and the age range stored against the product, I think this works out as needing to satisfy just one of the following inequalities-

userMinAge <= min_age <= userMaxAge OR

userMinAge <= max_age <= userMaxAge

I'm trying to run this using Yii's CDbCriteria and have come up with the following-

$criteria->condition = "$minAge <= min_age <= $maxAge OR $minAge <= max_age <= $maxAge";

This runs OK (as in no errors are thrown), but does not exclude products that it should. i.e a product with age range 8-19 was included when the user selected ages 20-35. Something is wrong, but my fairly average understanding of CDbCriteria is baffling me somewhat.

Can anyone spot why this approach isn't working? Are there any mistakes with my use of CDbCriteria?

Many thanks,

Goose

  • 写回答

1条回答 默认 最新

  • dsfgds4215 2013-06-16 17:57
    关注

    you can try

        $criteria->condition = "($minAge <= min_age <= $maxAge) OR ($minAge <= max_age <= $maxAge)";
    $criteria->condition = "(min_age BETWEEN $minAge AND $maxAge) OR(max_age BETWEEN $minAge AND $maxAge)";

    If this not resolve your problem please mention ur database schema, then It'll be easy to say whats going wrong...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)