and or php,AND OR 女爷i 2022-11-13 04:08 189阅读 0赞 \#\# AND OR 上面是基础的Where语句,下面看一下复杂一点的 你可以使用"AND" 或 "OR" 来拼接非常复杂的SQL语句 ~~~ namespace Action; use HY\\Action; class Index extends Action \{ public function Index()\{ //实例User表为对象 $User = S("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' // ) // ) \} \} ~~~
还没有评论,来说两句吧...