前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布

sql

作者头像
陈灬大灬海
发布2018-09-12 15:27:40
4470
发布2018-09-12 15:27:40
举报
文章被收录于专栏:面朝大海春暖花开

sql小语句总结(自娱自乐

简单的sqlwhereif查询

<select id="getListByWhere" parameterType="com.jiyun.beans.Employee" resultMap="haha">

select * from employee

<where>

<if test="id != null and id != ''">

and id = #{id}

</if>

<if test="name != null and name != ''">

and emp_name = #{name}

</if>

</where>

</select>

sql---foreach语句

<select id="getListByForeach" parameterType="list" resultMap="haha">

select * from employee where id in

<foreach collection="list" open="(" close=")" item="a" separator=",">

#{a}

</foreach>

</select>

sql---片段

<sql id="single">

where id=#{id};

</sql>

<select id="getEmployee" resultMap="haha" parameterType="int">

select * from employee

<include refid="single"></include>

</select>

sql---修改trim字段(功能最为强大)

<update id="updateByTrim" parameterType="com.jiyun.beans.Employee">

update employee

<trim prefix="set" suffixOverrides=",">

<if test="name != null and name != ''">

emp_name = #{name},

</if>

<if test="age != null and age != ''">

emp_age = #{age},

</if>

</trim>

where id=#{id};

</update>

<insert id="insertByTrim" parameterType="com.jiyun.beans.Employee">

insert into employee

<trim prefix="(" suffix=")" suffixOverrides=",">

<if test="name != null and name != ''">

emp_name,

</if>

<if test="age != null and age != ''">

emp_age,

</if>

</trim>

<trim prefix="values(" suffix=")" suffixOverrides=",">

<if test="name != null and name != ''">

#{name},

</if>

<if test="age != null and age != ''">

#{age},

</if>

</trim>

</insert>

sql语句一对多系列(说白了可以将代码复制一下)

<mapper namespace="com.jiyun.mapper.DeptMapper">

<!-- 使用resultMap进行映射 -->

<resultMap type="com.jiyun.beans.Dept" id="haha">

<id property="id" column="id"/>

<result property="deptName" column="dept_name"/>

<!-- 要表示一对多,需要用到collection标签 -->

<!-- property是集合的名字,ofType是集合元素的泛型

column把dept和employee关联起来,column表示用1的一端哪个列去关联多的一端

select写一条额外的sql语句,用某个mapper接口的方法的全类名表示-->

<collection property="list" ofType="com.jiyun.beans.Staff" column="id" select="com.jiyun.mapper.StaffMapper.getStaffByDeptId"></collection>

</resultMap>

<!-- 根据id查询一个部门 -->

<select id="getDeptById" parameterType="int" resultMap="haha">

select * from dept where id = #{id};

</select>

</mapper>

Select * From offence_bean Where DATE_FORMAT(new_date,'%m-%d') >= '04-20' and DATE_FORMAT(new_date,'%m-%d') <= '04-21'

select * from offence_bean where offence_city='运城' and new_date<'2018-1-20' and new_date>'2007-3-30'

select q.* from (SELECT a.*,money as m FROM (SELECT * FROM customers ORDER BY money DESC)a)q where m = 666;

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-05-03 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档