上一篇《Medoo入门:安装和配置-Medoo使用指南》中介绍了Medoo的安装、配置和基本使用。本篇将介绍Medoo的WHERE语法。Medoo的一些方法要求传递$where参数,像SQL的WHERE子句那样用于筛选查询记录。WHERE子句很强大,但有很多复杂的语法,逻辑相关性,以及有关SQL注入的潜在安全问题。但Medoo提供了强大和极端易用的方式来构造WHERE子句和预防SQL注入。
基本条件
基本条件足够简单易懂。您可以使用其他符号来获得用于数字的高级过滤器。
$database->select("account", "user_name", [ "email" => "[email protected]" ]); // WHERE email = '[email protected]' $database->select("account", "user_name", [ "user_id" => 200 ]); // WHERE user_id = 200 $database->select("account", "user_name", [ "user_id[>]" => 200 ]); // WHERE user_id > 200 $database->select("account", "user_name", [ "user_id[>=]" => 200 ]); // WHERE user_id >= 200 $database->select("account", "user_name", [ "user_id[!]" => 200 ]); // WHERE user_id != 200 $database->select("account", "user_name", [ "age[<>]" => [200, 500] ]); // WHERE age BETWEEN 200 AND 500 // 你不仅可以使用单一的字符串或数字值,也可以使用数组 $database->select("account", "user_name", [ "OR" => [ "user_id" => [2, 123, 234, 54], "email" => ["[email protected]", "[email protected]", "[email protected]"] ] ]); // WHERE // user_id IN (2,123,234,54) OR // email IN ('[email protected]','[email protected]','[email protected]') // 否定条件 (自Medoo 0.9起支持) $database->select("account", "user_name", [ "AND" => [ "user_name[!]" => "foo", "user_id[!]" => 1024, "email[!]" => ["[email protected]", "[email protected]", "[email protected]"], "city[!]" => null ] ]); // WHERE // `user_name` != 'foo' AND // `user_id` != 1024 AND // `email` NOT IN ('[email protected]','[email protected]','[email protected]') AND // `city` IS NOT NULL // 可以从 select()或get()方法的结果取得 $database->select("account", "user_name", [ "user_id" => $database->select("post", "user_id", ["comments[>]" => 40]) ]); // WHERE user_id IN (2, 51, 321, 3431)
相对条件
相对条件可以描述数据和数据之间的复杂关系。您可以使用“and”和“or”来构建复杂的相对条件查询。
// [基本的相对条件] $database->select("account", "user_name", [ "AND" => [ "user_id[>]" => 200, "age[<>]" => [18, 25], "gender" => "female" ] ]); // WHERE user_id > 200 AND age BETWEEN 18 AND 25 AND gender = 'female' $database->select("account", "user_name", [ "OR" => [ "user_id[>]" => 200, "age[<>]" => [18, 25], "gender" => "female" ] ]); // WHERE user_id > 200 OR age BETWEEN 18 AND 25 OR gender = 'female' // [复合的相对条件] $database->has("account", [ "AND" => [ "OR" => [ "user_name" => "foo", "email" => "[email protected]" ], "password" => "12345" ] ]); // WHERE (user_name = 'foo' OR email = '[email protected]') AND password = '12345'
全文搜索
通过目标关键词搜索记录。
// [MATCH] $database->select("post_table", "post_id", [ "MATCH" => [ "columns" => ["content", "title"], "keyword" => "foo" ] ]); // WHERE MATCH (content, title) AGAINST ('foo') // [LIKE] // The default connector of LIKE is AND $database->select("account", "user_id", [ 'LIKE' => [ 'location' => "foo", 'nickname' => "foo", 'user_name' => "foo", 'description' => "foo" ] ]); $database->select("account", "user_id", [ 'LIKE' => [ 'AND' => [ 'location' => "foo", 'nickname' => "foo", 'user_name' => "foo", 'description' => "foo" ] ] ]); // WHERE ( // location LIKE '%foo%' AND // nickname LIKE '%foo%' AND // user_name LIKE '%foo%' AND // description LIKE '%foo%' // ) $database->select("account", "user_id", [ 'LIKE' => [ 'OR' => [ 'location' => "foo", 'nickname' => "foo", 'user_name' => "foo", 'description' => "foo" ] ] ]); // WHERE ( // location LIKE '%foo%' OR // nickname LIKE '%foo%' OR // user_name LIKE '%foo%' OR // description LIKE '%foo%' // )
附加条件
$database->select("account", "user_id", [ "GROUP" => "type", // "ORDER" => "age DESC" "ORDER" => "age", // Must have to use it with ORDER together "HAVING" => [ "user_id[>]" => 500 ], // LIMIT => 20 "LIMIT" => [20, 100] ]); // SELECT user_id FROM account // GROUP BY type // ORDER BY age // HAVING user_id > 500 // LIMIT 20,100
原文标题:WHERE语法-Medoo使用指南
原文链接:http://loiy.net/post/566.html