IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> PHP知识库 -> PHP和JAVA 动态SQL -> 正文阅读

[PHP知识库]PHP和JAVA 动态SQL

Laravel动态SQL

  • 第一个:查询属性值


        $stay_at_school_id = request()->stay_at_school_id == null || request()->stay_at_school_id == "null" || request()->stay_at_school_id == "" ? null : (int)request()->stay_at_school_id;
        $check_state = request()->check_state == null || request()->check_state == "null" || request()->check_state == "" ? null : (int)request()->check_state;

 		//方便方便条件查询导出
        $result = StayAtSchoolDetails::with('stayAtSchool')
            ->with('myBed')->with('applyBed')
            ->when($stay_at_school_id, function ($query) use ($stay_at_school_id) {
            //whereHas 对象中对象属性可以用这个
                return $query->whereHas('stayAtSchool', function ($query) use ($stay_at_school_id) {
                    return $query->where('id', '=', $stay_at_school_id);
                });
            })
            //when(判断条件,回调函数),对象的属性值
            ->when($check_state==null, function ($query) use ($check_state) {
                return $query->where('check_state', '=', $check_state);
            })
            ->get();




  • 第二个:查询关联的属性值

查询关联的属性值
->whereHas('bed',function ($query) use ($bed_id){ $query->where('id','=',$bed_id); })


    function exportStudentData($request){

        //获取参数
        $name = $request['name']!="null"?substr($request['name'],1):null;
        $bed_id = $request['bed_id']!="null"?$request['bed_id']:null;
        $number = $request['number']!="null"?substr($request['number'],1):null;
        $nationality = $request['nationality']!="null"?substr($request['nationality'],1):null;
        $politics = $request['politics']!="null"?substr($request['politics'],1):null;
        $college = $request['college']!="null"?substr($request['college'],1):null;
        $major = $request['major']!="null"?substr($request['major'],1):null;
        $class = $request['class']!="null"?substr($request['class'],1):null;
        $grade = $request['grade']!="null"?substr($request['grade'],1):null;
        $system = $request['system']!="null"?substr($request['system'],1):null;
        $level = $request['level']!="null"?$request['level']:null;

        //获取数据
        $queryUserStudent =UserStudent::query()
            ->with('bed')
// 	业务不需要这个,用上一行代替。
//            ->whereHas('bed',function ($query) use ($bed_id){
//                $query->where('id','=',$bed_id);
//            })
            ->when($name,function ($query) use ($name){
                 $query->where('name','like',$name);
            })
            ->when($number,function ($query) use ($number){
                $query->where('number','like',$number);
            })
            ->when($nationality,function ($query) use ($nationality){
                $query->where('nationality','like',$nationality);
            })
            ->when($politics,function ($query) use ($politics){
                $query->where('politics','like',$politics);
            })
            ->when($college,function ($query) use ($college){
                $query->where('college','like',$college);
            })
            ->when($major,function ($query) use ($major){
                $query->where('major','like',$major);
            })
            ->when($class,function ($query) use ($class){
                $query->where('class','like',$class);
            })
            ->when($grade,function ($query) use ($grade){
                $query->where('grade','like',$grade);
            })
            ->when($system,function ($query) use ($system){
                $query->where('system','like',$system);
            })
            ->when($level,function ($query) use ($level){
                $query->where('level','=',$level);
            })
            ->get();

        return  $queryUserStudent;
    }
    



java 动态SQL


    <select id="selectAnswerList" parameterType="com.ruoyi.course.domain.Answer" resultMap="AnswerResult">
        select id, exam_id, student_number, content, score, create_time, update_time, delete_time,comment,state from answer
        <where>  
            <if test="examId != null "> and exam_id = #{examId}</if>
            <if test="studentNumber != null  and studentNumber != ''"> and student_number = #{studentNumber}</if>
            <if test="content != null  and content != ''"> and content = #{content}</if>
            <if test="score != null "> and score = #{score}</if>
            <if test="deleteTime != null "> and delete_time = #{deleteTime}</if>
            <if test="comment != null "> and comment = #{comment}</if>
            <if test="state != null "> and state = #{state}</if>
        </where>
    </select>

    <insert id="insertAnswer" parameterType="Answer" useGeneratedKeys="true" keyProperty="id">
        insert into answer
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="examId != null ">exam_id,</if>
            <if test="studentNumber != null  and studentNumber != ''">student_number,</if>
            <if test="content != null ">content,</if>
            <if test="score != null ">score,</if>
            <if test="createTime != null ">create_time,</if>
            <if test="updateTime != null ">update_time,</if>
            <if test="deleteTime != null ">delete_time,</if>
            <if test="comment != null ">comment,</if>
            <if test="state != null ">state,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="examId != null ">#{examId},</if>
            <if test="studentNumber != null  and studentNumber != ''">#{studentNumber},</if>
            <if test="content != null ">#{content},</if>
            <if test="score != null ">#{score},</if>
            <if test="createTime != null ">#{createTime},</if>
            <if test="updateTime != null ">#{updateTime},</if>
            <if test="deleteTime != null ">#{deleteTime},</if>
            <if test="comment != null ">#{comment},</if>
            <if test="state != null ">#{state},</if>
         </trim>
    </insert>


    <update id="updateAnswer" parameterType="com.ruoyi.course.domain.Answer">
        update answer
        <trim prefix="SET" suffixOverrides=",">
            <if test="examId != null ">exam_id = #{examId},</if>
            <if test="studentNumber != null  and studentNumber != ''">student_number = #{studentNumber},</if>
            <if test="content != null ">content = #{content},</if>
            <if test="score != null ">score = #{score},</if>
            <if test="createTime != null ">create_time = #{createTime},</if>
            <if test="updateTime != null ">update_time = #{updateTime},</if>
            <if test="deleteTime != null ">delete_time = #{deleteTime},</if>
            <if test="comment != null ">comment = #{comment},</if>
            <if test="state != null ">state = #{state},</if>
        </trim>
        where id = #{id}
    </update>


select * from answer
where student_number  in 
(select   student_number from   answer group by  student_number,exam_id  having count(student_number) > 1) 
and  exam_id in (SELECT exam_id from   answer group by  exam_id, student_number  having count(exam_id) > 1)
;
  PHP知识库 最新文章
Laravel 下实现 Google 2fa 验证
UUCTF WP
DASCTF10月 web
XAMPP任意命令执行提升权限漏洞(CVE-2020-
[GYCTF2020]Easyphp
iwebsec靶场 代码执行关卡通关笔记
多个线程同步执行,多个线程依次执行,多个
php 没事记录下常用方法 (TP5.1)
php之jwt
2021-09-18
上一篇文章      下一篇文章      查看所有文章
加:2021-11-26 08:40:07  更:2021-11-26 08:40:42 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/14 14:48:12-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码