年年有"余"

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2166|回复: 0

4、CURD中查询方式的一般使用

[复制链接]
  • TA的每日心情

    2024-10-15 10:05
  • 签到天数: 372 天

    [LV.9]以坛为家II

    发表于 2016-3-11 21:47:43 | 显示全部楼层 |阅读模式
    [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')");
    		

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    手机版|小黑屋|Archiver|iOS开发笔记 ( 湘ICP备14010846号 )

    GMT+8, 2024-11-24 08:12 , Processed in 0.048237 second(s), 21 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表