上一篇《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