[PHP] 纯文本查看 复制代码 <?php
1、基本查询方式
1.1、字符串
where("id = 0 and username = 'admin'");
1.2、数组
// SELECT * FROM `user` WHERE `id` = 0 AND `username` = 'admin'
$array['id'] = 0;
$array['username'] = "admin";
这种关系默认为and关系,如果要改成or则需要添加数组值"_logic"
// SELECT * FROM `user` WHERE `id` = 0 or `username` = 'admin'
$array['_logic'] = "or";
2、表达式查询方式
"GT"大于、"EQ"等于、"EGT"大于等于、"LT"小于、"ELT"小于等于、"NEQ"不等于
// SELECT * FROM `user` WHERE `id` > 0
$array['id'] = array("GT", 0);
// SELECT * FROM `user` WHERE `email` LIKE '%com'
$array['email'] = array("like", "%com");
// SELECT * FROM `user` WHERE `email` NOT LIKE '%com'
$array['email'] = array("notlike", "%com");
// SELECT * FROM `user` WHERE (`email` LIKE '%com' OR `email` LIKE '%yusian%')
$array['email'] = array("like", array("%com", "%yusian%"));
// SELECT * FROM `user` WHERE (`email` LIKE '%com' AND `email` LIKE '%yusian%')
$array['email'] = array("like", array("%com", "%yusian%"), "and");
// SELECT * FROM `user` WHERE `id` BETWEEN 5 AND 7
$array['id'] = array("between", array(5, 7));
// SELECT * FROM `user` WHERE `id` NOT BETWEEN 5 AND 7
$array['id'] = array("notbetween", array(5, 7));
// SELECT * FROM `user` WHERE `id` IN (4,5,7)
$array['id'] = array("in", array(4, 5, 7));
// SELECT * FROM `user` WHERE `id` NOT IN (4,5,7)
$array['id'] = array("notin", array(4, 5, 7));
3、区间查询
// SELECT * FROM `user` WHERE ( `id` > 1 AND `id` < 4 )
$data['id'] = array(array("GT", 1), array("LT", 4));
// SELECT * FROM `user` WHERE ( `id` > 10 OR `id` < 4 )
$data['id'] = array(array("GT", 10), array("LT", 4), 'or');
// SELECT * FROM `user` WHERE ( `username` LIKE 'admin%' OR `username` LIKE '%sian%' )
$data['username'] = array(array("like", "admin%"), array("like", "%sian%"), "or");
4、统计查询
4.1、count
// SELECT COUNT(*) AS tp_count FROM `user` LIMIT 1
$c = $m->count();
// SELECT COUNT(*) AS tp_count FROM `user` WHERE `id` > 4 LIMIT 1
$data['id'] = array("GT", 4);
$c = $m->where($data)->count();
4.2、max|min|avg|sum
// SELECT MAX(id) AS tp_max FROM `user` LIMIT 1
$c = $m->max('id');
5、SQL直接查询
5.1、query 主要处理读取数据(成功返回结果集,失败返回false)
// select * from user where id > 3
$array = $m->query("select * from user where id > 3");
5.2、execute 用于更新或写入操作(成功返回影响行数,失败返回false)
// insert into user(username) values("sql")
$array = $m->execute("insert into user(username) values('sql')");
|