数据库查询实例(findOne、findAll、where)
来源:互联网 发布:数据库管理员培训学费 编辑:程序博客网 时间:2024/06/05 07:43
findOne()和findAll():
// 查询key值为10的客户$customer = Customer::findOne(10);$customer = Customer::find()->where(['id' => 10])->one();
// 查询年龄为30,状态值为1的客户$customer = Customer::findOne(['age' => 30, 'status' => 1]);$customer = Customer::find()->where(['age' => 30, 'status' => 1])->one();
// 查询key值为10的所有客户$customers = Customer::findAll(10);$customers = Customer::find()->where(['id' => 10])->all();
// 查询key值为10,11,12的客户$customers = Customer::findAll([10, 11, 12]);$customers = Customer::find()->where(['id' => [10, 11, 12]])->all();
// 查询年龄为30,状态值为1的所有客户$customers = Customer::findAll(['age' => 30, 'status' => 1]);$customers = Customer::find()->where(['age' => 30, 'status' => 1])->all();
where()条件:
$customers = Customer::find()->where($cond)->all();
$cond写法举例:
// SQL: (type = 1) AND (status = 2).$cond = ['type' => 1, 'status' => 2] // SQL:(id IN (1, 2, 3)) AND (status = 2)$cond = ['id' => [1, 2, 3], 'status' => 2] //SQL:status IS NULL$cond = ['status' => null]
and 将不同的条件组合在一起,用法举例:
//SQL:`id=1 AND id=2`$cond = ['and', 'id=1', 'id=2']//SQL:`type=1 AND (id=1 OR id=2)`$cond = ['and', 'type=1', ['or', 'id=1', 'id=2']]
or
//SQL:`(type IN (7, 8, 9) OR (id IN (1, 2, 3)))`$cond = ['or', ['type' => [7, 8, 9]], ['id' => [1, 2, 3]]
not
//SQL:`NOT (attribute IS NULL)`$cond = ['not', ['attribute' => null]]
between、[not between] 用法相同
//SQL:`id BETWEEN 1 AND 10`$cond = ['between', 'id', 1, 10]
in、[not in] 用法类似
//SQL:`id IN (1, 2, 3)`$cond = ['in', 'id', [1, 2, 3]]//IN条件也适用于多字段$cond = ['in', ['id', 'name'], [['id' => 1, 'name' => 'foo'], ['id' => 2, 'name' => 'bar']]]//也适用于内嵌sql语句$cond = ['in', 'user_id', (new Query())->select('id')->from('users')->where(['active' => 1])]
like
//SQL:`name LIKE '%tester%'`$cond = ['like', 'name', 'tester']//SQL:`name LIKE '%test%' AND name LIKE '%sample%'`$cond = ['like', 'name', ['test', 'sample']]//SQL:`name LIKE '%tester'`$cond = ['like', 'name', '%tester', false]
exists、not exists 用法类似
//SQL:EXISTS (SELECT "id" FROM "users" WHERE "active"=1)$cond = ['exists', (new Query())->select('id')->from('users')->where(['active' => 1])]
此外,您可以指定任意运算符如下
//SQL:`id >= 10`$cond = ['>=', 'id', 10]//SQL:`id != 10`$cond = ['!=', 'id', 10]
0 0
- 数据库查询实例(findOne、findAll、where)
- 数据库查询 WHERE EXISTS
- THINKPHP 数据库查询where语句条件
- tp框架where条件查询数据库
- 查询数据库的实例
- 数据库实例查询
- 查询当前数据库实例
- 数据库查询实例(基础)
- hibernate 数据库查询 实例
- findAll
- oracle数据库中有数据 findone返回的为null
- C#之Linq、where()、FindAll()的区别
- Ruby on Rails 查询数据库 where 和 find 的区别
- 数据库多表查询之 where & INNER JOIN
- 数据库多表查询之 where & INNER JOIN
- 数据库多表查询之 where & INNER JOIN
- 数据库多表查询之 where & INNER JOIN
- 数据库多表查询之 where & INNER JOIN
- 反编译APK获取代码&资源
- ubuntu12.04下安装kcope
- PullToRefresh 下拉刷新菜单
- 10个顶级的CSS UI开源框架
- 【leetcode】【203】Remove Linked List Elements
- 数据库查询实例(findOne、findAll、where)
- Android中MVP框架的使用
- elasticsearch集群监控工具bigdesk
- FileInputStream不支持从网络路径获取数据
- nyoj-306-走迷宫【dfs】【好题】
- 添加path linux
- Install WPF Theme
- C语言中break和continue的区别
- 计算机视觉(ComputerVision, CV)相关领域的网站链接