## AND OR 上面是基础的Where语句,下面看一下复杂一点的 你可以使用"AND" 或 "OR" 来拼接非常复杂的SQL语句 ~~~ <?php namespace Action; use HY\Action; class Index extends Action { public function Index(){ //实例User表为对象 $User = M("User"); // 基础使用 $User->select("user_name", array( "AND" => array( "user_id[>]" => 200, "age[<>]" => array(18, 25), "gender" => "female" ) )); // WHERE user_id > 200 AND age BETWEEN 18 AND 25 AND gender = 'female' $User->select("user_name", array( "OR" => array( "user_id[>]" => 200, "age[<>]" => array(18, 25), "gender" => "female" ) )); // WHERE user_id > 200 OR age BETWEEN 18 AND 25 OR gender = 'female' // 复合条件 $User->has(array( "AND" => array( "OR" => array( "user_name" => "foo", "email" => "foo@bar.com" ), "password" => "12345" ) )); // WHERE (user_name = 'foo' OR email = 'foo@bar.com') AND password = '12345' // 注意 // 因为使用的是数组传参,所以下面这种用法是错误的。 // 可见 你有两个OR ,数组不可能存在两个相同索引. 所以你需要将另一个OR 加上一个注释 $User->select('*', array( "AND" => array( "OR" => array( "user_name" => "foo", "email" => "foo@bar.com" ), "OR" => array( "user_name" => "bar", "email" => "bar@foo.com" ) ) )); // [X] SELECT * FROM "account" WHERE ("user_name" = 'bar' OR "email" = 'bar@foo.com') 这是错误的示范 // 正确的方式是使用如下方式定义复合条件 $User->select('*', array( "AND #Actually, this comment feature can be used on every AND and OR relativity condition" => array( "OR #the first condition" => array( "user_name" => "foo", "email" => "foo@bar.com" ), "OR #the second condition" => array( "user_name" => "bar", "email" => "bar@foo.com" ) ) )); // SELECT * FROM "account" // WHERE ( // ( // "user_name" = 'foo' OR "email" = 'foo@bar.com' // ) // AND // ( // "user_name" = 'bar' OR "email" = 'bar@foo.com' // ) // ) } } ~~~