上一篇《Medoo入门:安装和配置-Medoo使用指南》中介绍了Medoo的安装、配置和基本使用。本篇将介绍Medoo的WHERE语法。Medoo的一些方法要求传递$where参数,像SQL的WHERE子句那样用于筛选查询记录。WHERE子句很强大,但有不少复杂的语法,逻辑相关性,以及有关SQL注入的潜在安全问题。但Medoo提供了强大和极端易用的方式来构造WHERE子句和预防SQL注入。php
基本条件
html
基本条件足够简单易懂。您能够使用其余符号来得到用于数字的高级过滤器。数组
$database->select("account", "user_name", [ "email" => "foo@bar.com" ]); // WHERE email = 'foo@bar.com' $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" => ["foo@bar.com", "cat@dog.com", "admin@medoo.in"] ] ]); // WHERE // user_id IN (2,123,234,54) OR // email IN ('foo@bar.com','cat@dog.com','admin@medoo.in') // 否认条件 (自Medoo 0.9起支持) $database->select("account", "user_name", [ "AND" => [ "user_name[!]" => "foo", "user_id[!]" => 1024, "email[!]" => ["foo@bar.com", "cat@dog.com", "admin@medoo.in"], "city[!]" => null ] ]); // WHERE // `user_name` != 'foo' AND // `user_id` != 1024 AND // `email` NOT IN ('foo@bar.com','cat@dog.com','admin@medoo.in') 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”来构建复杂的相对条件查询。post
// [基本的相对条件] $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" => "foo@bar.com" ], "password" => "12345" ] ]); // WHERE (user_name = 'foo' OR email = 'foo@bar.com') AND password = '12345'
全文搜索spa
经过目标关键词搜索记录。.net
// [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%' // )
附加条件code
$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使用指南htm
原文连接:http://loiy.net/post/566.htmlblog