场景一 $符号的使用
要查询 修改时间在 beginTime 和 endTime 之间的数据,同时查询时间 < beginTime的第一个, 和 > endTime的第一个。
脑洞打开,想着怎么通过一个sql来完成,再不济也可以通过一个sql,复用几次。
最后发现,查询 beginTime 和 endTime之间的只能单独用一个sql。 另外两个可以复用一个sql,代码如下
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16//传< TemplatePo littleTimeTemplate = templateDao.queryTempalteTime(templateUsedQueryDto, "<"); //传> TemplatePo littleTimeTemplate = templateDao.queryTempalteTime(templateUsedQueryDto, ">"); //mybatis代码, 这里用 ${} 接收 < 和 >, 用#{} 接收正常参数 <select id="queryTempalteTime" resultType="com.shuidihuzhu.cs.servicesummary.po.TemplatePo"> select t.* from template t left join template_skill_group_relation tsgr on t.id = tsgr.skill_group_id where tsgr.skill_group_id = #{templateUsedQueryDto.skillGroupId} and t.create_time ${s} #{templateUsedQueryDto.beginTime} order by create_time desc limit 1 </select>
mybatis中,#{} 是预编译, ${} 是直接编译,打印出来的代码如下
复制代码
1select t.* from template t left join template_skill_group_relation tsgr on t.id = tsgr.skill_group_id where tsgr.skill_group_id = ? and t.create_time < ? order by create_time desc limit 1
注意,${ } 的部分,直接显示出 "<", #{} 的部分先是通过? 占位,然后传参的方式
查询出数据库同一个字段,记录数大于1的记录
使用子查询和分组
复制代码
1
2
3
4
5
6
7
8
9# 通过对session_id 分组,然后查出记录重复的session_id # 关键字 grouping by + having select session_id from session_chat where session_id in (1,2,3) group by session_id having count(session_id)>1) ORDER BY session_id, create_time asc # 完整sql select session_id, agent_name from session_chat where session_id in (select session_id from session_chat where session_id in () group by session_id having count(session_id)>1) ORDER BY session_id, create_time asc;
关键字 grouping by + having
根据Where中in的顺序排序
复制代码
1
2
3
4
5
6SELECT * FROM tb_merchant AS t WHERE t.id IN (20,16,35,100,201,131) order by FIELD(id, 20,16,35,100,201,131);
最后
以上就是玩命星星最近收集整理的关于Mysql使用记录场景一 $符号的使用根据Where中in的顺序排序的全部内容,更多相关Mysql使用记录场景一 内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复