FleaPHP【数据库】 查询条件($conditions) 的写法
2011-04-03 16:45
501 查看
在FleaPHP中,凡是用到数据库查询的函数,都需要查询条件参数$conditions,现讲述用法如下:
举例:
view plaincopy to clipboardprint?
// $conditions 保存<b style=”color: black; background-color: rgb(153, 255, 153);”>查询</b>条件
$conditions = ‘level_ix > 1′;
// $tableOrders 是一个订单数据表的表数据入口对象
$order = $tableOrders->find($conditions, ‘created DESC’, ‘id, title, body’);
$conditions = array(‘username’ => ‘dualface’);
// $tableUsers 是一个用户信息数据表的表数据入口对象
$user = $tableUsers->find($conditions);
// $conditions 保存查询条件
$conditions = 'level_ix > 1';
// $tableOrders 是一个订单数据表的表数据入口对象
$order = $tableOrders->find($conditions, 'created DESC', 'id, title, body');
$conditions = array('username' => 'dualface');
// $tableUsers 是一个用户信息数据表的表数据入口对象
$user = $tableUsers->find($conditions);$conditions 参数可以是整数、字符串和数组三种类型:
1.如果 $conditions 参数是一个整数,则假定该整数为主键字段值。
view plaincopy to clipboardprint?
// <b style=”color: black; background-color: rgb(153, 255, 153);”>查询</b>主键字段值为1的记录
$user = $tableUsers->find(1);
// 如果主键字段名为“id”,则生成的where字句为“WHERE `id` = 1”
// 查询主键字段值为1的记录
$user = $tableUsers->find(1);
// 如果主键字段名为“id”,则生成的where字句为“WHERE `id` = 1”2.如果 $conditions 参数是一个字符串,则该字符串将直接作为查询条件,这种方式可以支持最灵活的查询条件。 例如:
view plaincopy to clipboardprint?
$conditions = ‘id < 3′
$user = $tableUsers->find($conditions);
//生成的where字句为“WHERE id < 3”
$conditions = 'id < 3'
$user = $tableUsers->find($conditions);
//生成的where字句为“WHERE id < 3”3.1.如果 $conditions 参数是一个数组,且指定了键名和值,则查询条件中字段名为键名,字段值等于键值。例如:
view plaincopy to clipboardprint?
// <b style=”color: black; background-color: rgb(153, 255, 153);”>查询</b>id字段值为3的记录
$conditions = array(
‘id’ => ‘1′,
);
$user = $tableUsers->find($conditions);
//生成的where字句为“WHERE `id` = 1”
// 查询id字段值为3的记录
$conditions = array(
'id' => '1',
);
$user = $tableUsers->find($conditions);
//生成的where字句为“WHERE `id` = 1”3.2.如果 $conditions 参数是一个数组,但其中的元素没有键名, 则假定键值为自定义查询条件,例如:
view plaincopy to clipboardprint?
$conditions = array(‘id = 1′);
// 生成的where字句为“WHERE `id` = 1”
$user = $tableUsers->find($conditions);
$conditions = array('id = 1');
// 生成的where字句为“WHERE `id` = 1”
$user = $tableUsers->find($conditions);3.3.$conditions 为数组时,可以混用字符串和键值对两种风格:
view plaincopy to clipboardprint?
$conditions = array(
‘id < 3′,
’sex’ => ‘male’,
);
$user = $tableUsers->find($conditions);
// 生成的where字句为“id < 3 AND `sex` = ‘male’”
$conditions = array(
'id < 3',
'sex' => 'male',
);
$user = $tableUsers->find($conditions);
// 生成的where字句为“id < 3 AND `sex` = 'male'”$conditions 为数组时,多个查询条件之间将使用 AND 布尔运算符进行连接。
3.4.“in()”查询在FleaPHP中的实现。(原文由DreamPig发表于http://www.fleaphp.org/bbs/viewthread.php?tid=2168)
我们有时候要用到in这样的操作,那么在condition里面怎么写呢?
view plaincopy to clipboardprint?
// 假如主键名为“id”,需要<b style=”color: black; background-color: rgb(153, 255, 153);”>查询</b>id的值为1、2、3其中之一,则可以这样写:
$condition = array(
‘in()’ => array(1,2,3),
)
$user = $tableUsers->find($conditions);
// 生成的where子句为“WHERE `id` IN (1, 2, 3)”
// 假如主键名为“id”,需要查询id的值为1、2、3其中之一,则可以这样写:
$condition = array(
'in()' => array(1,2,3),
)
$user = $tableUsers->find($conditions);
// 生成的where子句为“WHERE `id` IN (1, 2, 3)”那么如果不是主键的话怎么写了呢? 也很简单,提供键值对即可。例如:
view plaincopy to clipboardprint?
$condition = array(
‘in()’ => array(
‘username’ => array(‘username1′,‘username2′)
)
)
$user = $tableUsers->find($conditions);
// 生成的where子句为“WHERE `username` IN (’username1′, ‘username2′)”
$condition = array(
'in()' => array(
'username' => array('username1','username2')
)
)
$user = $tableUsers->find($conditions);
// 生成的where子句为“WHERE `username` IN ('username1', 'username2')”4.find()函数中其它参数的含义和用法如下:
4.1.$sort 参数指定查询时的排序方式,类型只能为字符串
例如 ‘created ASC’ 表示按照“created”字段进行从小到大的排序。
4.2.$fields 参数指定查询结果中要包含哪些字段,类型可以为字符串或数组
当数据表的字段很多时,通过指定 $fields 参数可以避免查询不需要的字段,从而提高性能。
$fields 参数即可是以“,”逗号分隔的字段名,也可以是包含多个字段名的数组,例如:
view plaincopy to clipboardprint?
$fields = array(‘title’, ‘created’);
//也可以写成下面的字符串形式,两种写法作用相同,区别在于自动生成的字段名两边将会添加上“`”符号,以防止出现字段名与SQL关键字冲突的情况出现。建议手写时也加上“`”字符
$fields = ‘title, created’;
$user = $tableUsers->find(‘id < 10′,NULL,$fields);
$fields = array('title', 'created');
//也可以写成下面的字符串形式,两种写法作用相同,区别在于自动生成的字段名两边将会添加上“`”符号,以防止出现字段名与SQL关键字冲突的情况出现。建议手写时也加上“`”字符
$fields = 'title, created';
$user = $tableUsers->find('id < 10',NULL,$fields);推荐使用数组,这样表数据入口处理起来更快一些。
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/phpsome/archive/2009/03/19/4006069.aspx
举例:
view plaincopy to clipboardprint?
// $conditions 保存<b style=”color: black; background-color: rgb(153, 255, 153);”>查询</b>条件
$conditions = ‘level_ix > 1′;
// $tableOrders 是一个订单数据表的表数据入口对象
$order = $tableOrders->find($conditions, ‘created DESC’, ‘id, title, body’);
$conditions = array(‘username’ => ‘dualface’);
// $tableUsers 是一个用户信息数据表的表数据入口对象
$user = $tableUsers->find($conditions);
// $conditions 保存查询条件
$conditions = 'level_ix > 1';
// $tableOrders 是一个订单数据表的表数据入口对象
$order = $tableOrders->find($conditions, 'created DESC', 'id, title, body');
$conditions = array('username' => 'dualface');
// $tableUsers 是一个用户信息数据表的表数据入口对象
$user = $tableUsers->find($conditions);$conditions 参数可以是整数、字符串和数组三种类型:
1.如果 $conditions 参数是一个整数,则假定该整数为主键字段值。
view plaincopy to clipboardprint?
// <b style=”color: black; background-color: rgb(153, 255, 153);”>查询</b>主键字段值为1的记录
$user = $tableUsers->find(1);
// 如果主键字段名为“id”,则生成的where字句为“WHERE `id` = 1”
// 查询主键字段值为1的记录
$user = $tableUsers->find(1);
// 如果主键字段名为“id”,则生成的where字句为“WHERE `id` = 1”2.如果 $conditions 参数是一个字符串,则该字符串将直接作为查询条件,这种方式可以支持最灵活的查询条件。 例如:
view plaincopy to clipboardprint?
$conditions = ‘id < 3′
$user = $tableUsers->find($conditions);
//生成的where字句为“WHERE id < 3”
$conditions = 'id < 3'
$user = $tableUsers->find($conditions);
//生成的where字句为“WHERE id < 3”3.1.如果 $conditions 参数是一个数组,且指定了键名和值,则查询条件中字段名为键名,字段值等于键值。例如:
view plaincopy to clipboardprint?
// <b style=”color: black; background-color: rgb(153, 255, 153);”>查询</b>id字段值为3的记录
$conditions = array(
‘id’ => ‘1′,
);
$user = $tableUsers->find($conditions);
//生成的where字句为“WHERE `id` = 1”
// 查询id字段值为3的记录
$conditions = array(
'id' => '1',
);
$user = $tableUsers->find($conditions);
//生成的where字句为“WHERE `id` = 1”3.2.如果 $conditions 参数是一个数组,但其中的元素没有键名, 则假定键值为自定义查询条件,例如:
view plaincopy to clipboardprint?
$conditions = array(‘id = 1′);
// 生成的where字句为“WHERE `id` = 1”
$user = $tableUsers->find($conditions);
$conditions = array('id = 1');
// 生成的where字句为“WHERE `id` = 1”
$user = $tableUsers->find($conditions);3.3.$conditions 为数组时,可以混用字符串和键值对两种风格:
view plaincopy to clipboardprint?
$conditions = array(
‘id < 3′,
’sex’ => ‘male’,
);
$user = $tableUsers->find($conditions);
// 生成的where字句为“id < 3 AND `sex` = ‘male’”
$conditions = array(
'id < 3',
'sex' => 'male',
);
$user = $tableUsers->find($conditions);
// 生成的where字句为“id < 3 AND `sex` = 'male'”$conditions 为数组时,多个查询条件之间将使用 AND 布尔运算符进行连接。
3.4.“in()”查询在FleaPHP中的实现。(原文由DreamPig发表于http://www.fleaphp.org/bbs/viewthread.php?tid=2168)
我们有时候要用到in这样的操作,那么在condition里面怎么写呢?
view plaincopy to clipboardprint?
// 假如主键名为“id”,需要<b style=”color: black; background-color: rgb(153, 255, 153);”>查询</b>id的值为1、2、3其中之一,则可以这样写:
$condition = array(
‘in()’ => array(1,2,3),
)
$user = $tableUsers->find($conditions);
// 生成的where子句为“WHERE `id` IN (1, 2, 3)”
// 假如主键名为“id”,需要查询id的值为1、2、3其中之一,则可以这样写:
$condition = array(
'in()' => array(1,2,3),
)
$user = $tableUsers->find($conditions);
// 生成的where子句为“WHERE `id` IN (1, 2, 3)”那么如果不是主键的话怎么写了呢? 也很简单,提供键值对即可。例如:
view plaincopy to clipboardprint?
$condition = array(
‘in()’ => array(
‘username’ => array(‘username1′,‘username2′)
)
)
$user = $tableUsers->find($conditions);
// 生成的where子句为“WHERE `username` IN (’username1′, ‘username2′)”
$condition = array(
'in()' => array(
'username' => array('username1','username2')
)
)
$user = $tableUsers->find($conditions);
// 生成的where子句为“WHERE `username` IN ('username1', 'username2')”4.find()函数中其它参数的含义和用法如下:
4.1.$sort 参数指定查询时的排序方式,类型只能为字符串
例如 ‘created ASC’ 表示按照“created”字段进行从小到大的排序。
4.2.$fields 参数指定查询结果中要包含哪些字段,类型可以为字符串或数组
当数据表的字段很多时,通过指定 $fields 参数可以避免查询不需要的字段,从而提高性能。
$fields 参数即可是以“,”逗号分隔的字段名,也可以是包含多个字段名的数组,例如:
view plaincopy to clipboardprint?
$fields = array(‘title’, ‘created’);
//也可以写成下面的字符串形式,两种写法作用相同,区别在于自动生成的字段名两边将会添加上“`”符号,以防止出现字段名与SQL关键字冲突的情况出现。建议手写时也加上“`”字符
$fields = ‘title, created’;
$user = $tableUsers->find(‘id < 10′,NULL,$fields);
$fields = array('title', 'created');
//也可以写成下面的字符串形式,两种写法作用相同,区别在于自动生成的字段名两边将会添加上“`”符号,以防止出现字段名与SQL关键字冲突的情况出现。建议手写时也加上“`”字符
$fields = 'title, created';
$user = $tableUsers->find('id < 10',NULL,$fields);推荐使用数组,这样表数据入口处理起来更快一些。
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/phpsome/archive/2009/03/19/4006069.aspx
相关文章推荐
- FleaPHP框架数据库查询条件($conditions)写法总结
- FleaPHP框架数据库查询条件($conditions)写法总结
- FleaPHP【数据库】 查询条件($conditions) 的写法
- python 中 MySQLdb 操作数据库,查询条件为元组时写法。
- 数据库查询语句,追加条件的写法。
- MySQL左联多表查询where条件写法示例
- 数据库左连接left join、右连接right join、内连接inner join on 及 where条件查询的区别
- 【Java EE 学习 17 下】【数据库导出到Excel】【多条件查询方法】
- oracle数据库使用时间作为查询条件时的写法
- hibernate面试题 --- 在数据库中条件查询很慢的时候,如何优化
- 在数据库中条件查询很慢的时候,如何优化
- PHP查询数据库中满足条件的记录条数(两种实现方法)
- 查询日期范围时SQL条件的写法
- tp框架where条件查询数据库
- Oracle clob类型的字段作为查询条件的sql语句写法
- fleaphp下不确定的多条件查询的巧妙解决方法
- 黑马程序员_学习日记49_618数据库开发及ADO.Net(多条件搜索、case…when…语句、索引Index、子查询、分页、Join语句)
- 自己总结的不定条件SQL查询语句的写法
- 多条件数据库查询的优化方法
- 从索引技术谈数据库查询索引建立和查询条件书写