WHERE 语句

SQL中使用where可能会有一些不安全的动态参数传入或者一些复杂的SQL语句,但是Medoo提供非常简介和安全的方法来实现这些.

基本使用

在基本使用中. 你可以使用一些符号对参数进行过滤$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// [>] is also available for 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-01')$database->select("account", "user_name", ["birthday[> [date("Y-m-d", mktime(0, 0, 0, 1, 1, 2015)), date("Y-m-d")]]);// WHERE ("birthday" NOT BETWEEN '2015-01-01' AND '2017-01-01')// You can use not only single string or number value, but also array$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')// [Negative condition]$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// Or fetched from select() or get() function$database->select("account", "user_name", ["user_id" => $database->select("post", "user_id", ["comments[>]" => 40])]);// WHERE user_id IN (2, 51, 321, 3431)

带有相对条件使用$database->select("account", "user_name", ["AND" => ["user_id[>]" => 200,"age[<>]" => [18, 25],"gender" => "female"]]);// Medoo 默认使用AND连接各个条件$database->select("account", "user_name", ["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'// [IMPORTANT]// Because Medoo is using array data construction to describe relativity condition,// array with duplicated key will be overwritten. This will be error:$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')// To correct that, just assign a comment for each AND and OR key name. The comment content can be everything.$database->select("account", '*', ["AND #Actually, this comment feature can be used on every AND and OR relativity condition" => ["OR #the first condition" => ["user_name" => "foo","email" => "foo@bar.com"],"OR #the second condition" => ["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'// )// )

列的关系处理$database->select("post", ["[>]account" => ["author_id" => "user_id"],], ["post.id","post.content"], ["AND" => [// Connect two column with condition sign like [=], [>], [ "foo","account.email" => "foo@bar.com",]]);// WHERE "post"."restrict" < "account"."age" AND "account"."user_name" = 'foo' AND "account"."email" = 'foo@bar.com'

模糊匹配 like

LIKE 使用语法 [~] .// 默认情况下,使用%在前后包含关键词$database->select("person", "id", ["city[~]" => "lon"]);WHERE "city" LIKE '%lon%'// Array support$database->select("person", "id", ["city[~]" => ["lon", "foo", "bar"]]);WHERE "city" LIKE '%lon%' OR "city" LIKE '%foo%' OR "city" LIKE '%bar%'// Negative condition [!~]$database->select("person", "id", ["city[!~]" => "lon"]);WHERE "city" NOT LIKE '%lon%'

通配符的使用// You can use SQL wildcard to match more complex situation$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("person", "id", ["content[~]" => ["AND" => ["lon", "on"]]]);// WHERE ("content" LIKE '%lon%' AND "content" LIKE '%on%')$database->select("person", "id", ["content[~]" => ["OR" => ["lon", "on"]]]);// WHERE ("content" LIKE '%lon%' OR "content" LIKE '%on%')

排序$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"]]);

全文检索

MySQL支持全文检索高级查询naturalIN NATURAL LANGUAGE MODE

natural+queryIN NATURAL LANGUAGE MODE WITH QUERY EXPANSION

booleanIN BOOLEAN MODE

queryWITH QUERY EXPANSION// [MATCH]$database->select("post_table", "post_id", ["MATCH" => ["columns" => ["content", "title"],"keyword" => "foo",// [optional] Search mode"mode" => "natural"]]);// WHERE MATCH (content, title) AGAINST ('foo' IN NATURAL LANGUAGE MODE)

使用正则$data = $database->select('account', ['user_id','user_name'], ['user_name[REGEXP]' => '[a-z0-9]*']);// SELECT "user_id","user_name"// FROM "account"// WHERE "user_name" REGEXP '[a-z0-9]*'

使用SQL函数

处理SQL内置函数的方法在1.2之后做了修改,与以前的不兼容。详情可阅读 Raw object章节$data = $database->select('account', ['user_id','user_name'], ['datetime' => Medoo::raw('NOW()')]);// SELECT "user_id","user_name"// FROM "account"// WHERE "datetime" = NOW()

LIMIT 和 OFFSET$database->select("account", "user_id", [// Get the first 100 of rows'LIMIT' => 100// Started from the top 20 rows, and get the next 100'LIMIT' => [20, 100],// For Oracle and MSSQL database, you also need to use with GROUP by together'GROUP' => 'location']);

GROUP 和 HAVING $database->select("account", "user_id", ['GROUP' => 'type',// GROUP by array of values'GROUP' => ['type','age','gender'],// Must have to use it with GROUP together'HAVING' => ['user_id[>]' => 500]]);

php medoo好用吗,where 查询条件-Medoo - 高效的轻量级PHP数据库框架, 提高开发效率!...相关推荐

  1. medoo php,pdo-Medoo - 高效的轻量级PHP数据库框架, 提高开发效率!

    Raw Object Medoo可以使用原始表达式来进行复杂的或自定义的查询,尤其是在使用SQL内置函数的时候.支持占位符号,以防止注入和优化语法. Medoo::raw($query, $map) ...

  2. php mysql 条件查询语句_where 查询条件-Medoo - 高效的轻量级PHP数据库框架, 提高开发效率!...

    WHERE 语句 SQL中使用where可能会有一些不安全的动态参数传入或者一些复杂的SQL语句,但是Medoo提供非常简介和安全的方法来实现这些. 基础使用 在基础使用中. 你可以使用一些符号对参数 ...

  3. php删除框架集,delete 删除-Medoo - 高效的轻量级PHP数据库框架, 提高开发效率!

    delete 删除表中的数据 delete($table, $where)table [string] 表名. where [array] WHERE 删除条件. Return: [PDOStatem ...

  4. 关于模糊查询时的索引问题.(了解一下,对提高代码效率非常有好处)

    今天看SQL规约.其中一条说,使用 like '%om%'时,由于前面的%,我们无法使用索引. 如果想使用索引,就要这么写like 'tom%'.

  5. JEECG - 基于代码生成器的J2EE智能开发框架 续四: 查询条件SQL生成器设计思路

    JEECG[J2EE  Code Generation]是一款基于代码生成器的敏捷开发框架. 续前文:http://blog.csdn.net/zhangdaiscott/article/detail ...

  6. MybatisPlus QueryWrapper(简称 QW,MP 封装的一个查询条件构造器)的使用和简单认识

    上一篇讲的是MybatisPlus的MP注解用法和简单介绍 传送门 (跟上一篇是同一个项目)先来创一个Springboot测试项目 创建一个数据库 -- 创建表 CREATE TABLE t_empl ...

  7. python 数据库查询条件‘不等于’

    python 数据库查询条件'不等于' 当在做数据库查询的时候,想根据业务需求进行条件的筛选或过滤, 但是django封装的数据库语句中没有 '不等于' 查询操作. 例如,通过以下语句进行'不等于查询 ...

  8. 按拼音模糊匹配查询条件的生成类

    转载了好几个地方,很难确定最早的出处. 将源码贴出来先.  1using System;   2using System.Text;   3using System.IO;   4  5namespa ...

  9. 使用ajax实现多个查询条件功能以及下拉分页

    2018-8月18日 1.效果图: 2.前台html <!--搜索栏-->             <div class=" row " style=" ...

最新文章

  1. 蓝桥杯-答疑-java
  2. C语言基本数据结构之一(线性链表的增,删,改,查及倒序)
  3. C语言求m中n个数字的组合
  4. python1000个常用代码-1000个常用的Python库和示例代码
  5. Ksusha and Array (vector)
  6. python管理工具ports_Python options.port方法代码示例
  7. RPC 技术及其框架 Sekiro 在爬虫逆向中的应用,加密数据一把梭
  8. 认识伪类元素:before和:after
  9. Spring配置中的bean直接引用其它bean的属性值
  10. tomcat servlet 线程
  11. JDBC下载及连接数据库处理
  12. 最小生成树详解(模板 + 例题)
  13. 各国个人信息安全立法进度
  14. 程序员有必要掌握 TDD 吗?
  15. IP地址的定义及分类
  16. 做时间的朋友,必须知道收益咋算
  17. How to reassign lifecycle in Windchill
  18. redis数据库的概述
  19. SVN迁移至GIT,并附带历史提交记录
  20. 算法总结——大整数乘法

热门文章

  1. 华南理工大学软件学院2016考研复试机试第一题代码(Java)
  2. 若int a = 0, b = 1, c = 2,则逻辑表达式a++ b++ || (c -= 2)执行之后
  3. 如何删除 excel 单元格内的换行符
  4. excel处理时间数据
  5. 算法模板(5):数学(3):多项式1
  6. 苹果画画软件_苹果要求儿童类App不得含广告,我们测了100款,5款仍存在
  7. 扩展卡尔曼滤波建模及应用
  8. hash在后端的应用
  9. Python数据分析-FIFA2018球员数据分析
  10. DirectX11 With Windows SDK--08 Direct2D与Direct3D互操作性以及利用DWrite显示文字