doutu9810 2013-06-16 07: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 09: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...

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

报告相同问题?