php mysql 条件查询语句_where 查询条件-Medoo - 高效的轻量级PHP数据库框架, 提高开发效率!...
WHERE 語句
SQL中使用where可能會有一些不安全的動態(tài)參數(shù)傳入或者一些復(fù)雜的SQL語句,但是Medoo提供非常簡介和安全的方法來實現(xiàn)這些.
基礎(chǔ)使用
在基礎(chǔ)使用中. 你可以使用一些符號對參數(shù)進行過濾$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", [ "age[> [200, 500]]);// WHERE age NOT BETWEEN 200 AND 500// [>] 可以用于 datetime$database->select("account", "user_name", [ "birthday[> [date("Y-m-d", mktime(0, 0, 0, 1, 1, 2015)), date("Y-m-d")]]);//WHERE `birthday` BETWEEN '2015-01-01' AND '2017-01-16' (now)// 你不僅可以使用字符串和數(shù)字,還可以使用數(shù)組$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')// 多條件查詢$database->select("account", "user_name", [ "AND" => [ "user_name[!]" => "foo", "user_id[!]" => 1024, "email[!]" => ["foo@bar.com", "cat@dog.com", "admin@medoo.in"], "city[!]" => null, "promoted[!]" => true ]]);// 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// `promoted` != 1// 或者嵌套 select() ak 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" 來拼接非常復(fù)雜的SQL語句// 基礎(chǔ)使用$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'// 復(fù)合條件$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'// 注意// 因為medoo使用的是數(shù)組傳參,所以下面這種用法是錯誤的。$database->select("account", '*', [ "AND" => [ "OR" => [ "user_name" => "foo", "email" => "foo@bar.com" ], "OR" => [ "user_name" => "bar", "email" => "bar@foo.com" ] ]]);// [X] SELECT * FROM "account" WHERE ("user_name" = 'bar' OR "email" = 'bar@foo.com')// 正確的方式是使用如下方式定義復(fù)合條件$database->select("account", '*', [ "AND" => [ //實際應(yīng)用時這兒可以使用AND或者OR "OR" => [ //第一個條件 "user_name" => "foo", "email" => "foo@bar.com" ], "OR" => [ //第二個條件 "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'// )// )
模糊匹配 like
LIKE 使用語法 [~] .// 默認情況下,使用%在前后包含關(guān)鍵詞$database->select("person", "id", [ "city[~]" => "lon"]);WHERE "city" LIKE '%lon%'// 數(shù)組形式,查詢多個關(guān)鍵詞$database->select("person", "id", [ "city[~]" => ["lon", "foo", "bar"]]);WHERE "city" LIKE '%lon%' OR "city" LIKE '%foo%' OR "city" LIKE '%bar%'// 不包含 [!~]$database->select("person", "id", [ "city[!~]" => "lon"]);WHERE "city" NOT LIKE '%lon%'// 使用SQL自帶的一些通配符// 你可以使用sql自帶的一些通配符來完成較復(fù)雜的查詢$database->select("person", "id", [ "city[~]" => "stan%" // Kazakhstan, Uzbekistan, Türkmenistan]);$database->select("person", "id", [ "city[~]" => "Londo_" // London, Londox, Londos...]);$database->select("person", "id", [ "name[~]" => "[BCR]at" // Bat, Cat, Rat]);$database->select("person", "id", [ "name[~]" => "[!BCR]at" // Eat, Fat, Hat...]);
排序使用$database->select("account", "user_id", [ // Single condition"ORDER" => "user_id", // Multiple condition"ORDER" => [// Order by column with sorting by customized order."user_id" => [43, 12, 57, 98, 144, 1], // Order by column"register_date", // Order by column with descending sorting"profile_id" => "DESC", // Order by column with ascending sorting"date" => "ASC"]]);
全文檢索// [MATCH]$database->select("post_table", "post_id", [ "MATCH" => [ "columns" => ["content", "title"], "keyword" => "foo" ]]);// WHERE MATCH (content, title) AGAINST ('foo')
使用SQL函數(shù)
在一些特殊的情況下,你可能需要使用SQL系統(tǒng)函數(shù),只需要字段名前加上#號即可$data = $database->select('account', [ 'user_id', 'user_name'], [ '#datetime' => 'NOW()']);// SELECT "user_id","user_name"// FROM "account"// WHERE "datetime" = NOW()// [IMPORTANT] Keep in mind that, the value will not be quoted should be matched as XXX() uppercase.// The following sample will be failed.$database->select('account', [ 'user_id', 'user_name'], [ '#datetime2' => 'now()', 'datetime3' => 'NOW()', '#datetime4' => 'NOW']);
其它參數(shù)$database->select("account", "user_id", [ "GROUP" => "type", // Must have to use it with GROUP together "HAVING" => [ "user_id[>]" => 500 ], // LIMIT => 20 "LIMIT" => [20, 100]]);// SELECT user_id FROM account// GROUP BY type// HAVING user_id > 500// LIMIT 20,100
總結(jié)
以上是生活随笔為你收集整理的php mysql 条件查询语句_where 查询条件-Medoo - 高效的轻量级PHP数据库框架, 提高开发效率!...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 服务器响应时间过长_网站响应时间过长怎么
- 下一篇: linux cmake编译源码,linu