find_in_set的使用

本文介绍了一种基于SQL的查询方法,通过设置不同的变量如$mid和$department_id来实现对数据库表中特定记录的筛选。具体实现了根据不同类型的请求加载对应的数据记录,并通过$order参数确定返回结果的排序方式。

表结构:

代码:

查询包今$mid和$department_id的记录

$mid = cookie('mid');
$department_id = M('user')->where('id = ' . $mid)->getField('department_id');
if(!$type) {
                //首次加载首页
                $where = 'is_send = 1 and to_type = 1 or find_in_set(' . $mid . ',to_mid) or find_in_set(' . $department_id . ',to_department)';
            } elseif ($type == 1) {
                $new_id = I('post.new_li','','intval');
                $where = 'id > ' . $new_id;
                $where .= ' and (is_send = 1 and to_type = 1 or find_in_set(' . $mid . ',to_mid) or find_in_set(' . $department_id . ',to_department))';
            } elseif($type == 2) {
                $page = I('post.page',2,'intval');
                $where = 'is_send = 1 and to_type = 1 or find_in_set(' . $mid . ',to_mid) or find_in_set(' . $department_id . ',to_department)';
            }

            $order = 'add_time desc';//is_top desc,
            $list = M('notice')->where($where)->order($order)->page($page, $limit)->select();

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值