09-06Sql的一些问题


in关键字

指定多个id来删除数据 比如1,2,3
只要是id这这几个数的范围内的删除掉,,这!就是in! 好屌啊
delete from employee where id in (1,2,3)


嵌套查询

将查询出来的删除
delete from employee where id in (select id from employee where 条件)


${}和#{}的区别

获取参数的值在本质上是通过get方法获取值
在mybatis中${} 获取的内容原样输出,相当于进行字符串的拼接生成sql语句,一般情况下,优先使用#{}。在使用${}的时候参数的类型一般为map。parameterType="map"

例如在这里,要查询表中所有的信息,如果参数类型为String类型的话,会报错说在String类型中没有getter方法
<select id="findAll2" parameterType="map" resultType="Person">
若是使用井号修饰输出的sql语句是这样的明显不对        
<!-- select * from #{table}  select * from 'person' -->
        select * from ${table}
</select>

map

mybatis中,在映射文件中的sql语句所使用的参数基本都会封装为一个map对象(key,value)
基本类型(类似string这些):key值可以随便写
自定义类的对象: key 属性, value 设置的值
数组: key值是array,value是数组对象。在设置参数类型必须设置为map对象
列表:key值是list,value是列表对象


<insert id="add1" parameterType="Department">
    <!-- 插入数据后order="AFTER",通过last_insert_id() 获取自增的值,赋值给did属性 -->
        <selectKey keyProperty="did" order="AFTER" resultType="int">
            SELECT LAST_INSERT_ID()
        </selectKey>
        insert into t_dept(dname) values(#{dname})
    </insert>

in关键字

指定多个id来删除数据 比如1,2,3
只要是id这这几个数的范围内的删除掉,,这!就是in! 好屌啊
delete from employee where id in (1,2,3)


嵌套查询

将查询出来的删除
delete from employee where id in (select id from employee where 条件)

在mybatis中和#修饰{属性}的区别,{} 获取的内容原样输出,相当于进行字符串的拼接生成sql语句,一般情况下,优先使用#{}。在使用${}的时候参数的类型一般为map。parameterType="map"


在绑定数据时,(表中的字段为age)发现实体类中没有age属性(没有age的get和set方法),所以无法绑定
可以通过字段的别名解决,别名为实体类中的属性
<select id="findByName2" parameterType="string" resultType="Person">
        select id,name,age as perage from person where name=#{name}
</select>

动态sql

 <!-- resultMap 结果映射,对应的值是<resultMap>标签的id值 -->
    <!-- 当类中的属性名和表中的字段名不一致时,要想成功绑定数据,就需要使用resultMap进行映射-->
在这里可能表中的字段名与实体类中的属性名并不相同,但是通过映射可以绑定数据
    <select id="findByName" parameterType="string" resultMap="personMap">
        select id,name,age from person where name=#{name}
    </select>
使用<if>标签进行判断
<insert id="add" parameterType="employee">
        insert into employee(
            <trim suffixOverrides=",">
            <if test="name!=null">name,</if>            
            <if test="age!=null">age,</if>          
            <if test="phone!=null">phone,</if>          
            <if test="qq!=null">qq,</if>            
            </trim>
        ) values(
            <trim suffixOverrides=",">
            <if test="name!=null">#{name},</if>         
            <if test="age!=null">#{age},</if>           
            <if test="phone!=null">#{phone},</if>           
            <if test="qq!=null">#{qq},</if>
            </trim>
        )
    </insert>
<select id="find" parameterType="Employee" resultType="Employee">
        <!-- select * from employee where 1=1 and name=? and age=? -->  
        select * from employee
        <!-- 将多余的前缀,比如and,自动删掉 -->
        <where>
            <if test="name!=null">and name=#{name}</if>
            <if test="age!=null">and age=#{age}</if>
        </where>
    </select>
<update id="update" parameterType="Employee">
        <!-- update employee set name=?,age=?,phone=? where .. -->
        update employee 
        <!-- 自动将多余的逗号删除 -->
        <set>
            <if test="name!=null">name=#{name},</if>
            <if test="age!=null">age=#{age},</if>
            <if test="phone!=null">phone=#{phone},</if>
            <if test="qq!=null">qq=#{qq},</if>
        </set>
        where id=#{id}
</update>

在使用多个参数的时候

sql中使用的参数,本质上会封装为map对象
基本类型 key值随便写
自定义类的对象: key 属性, value 设置的值
数组: key值是array,value是数组对象;
列表:key值是list,value是列表对象
map结构

<delete id="deleteByIds" parameterType="list">
        <!-- delete from employee where id in (1, 2, 6) -->
        delete from employee where id in
        <!-- 遍历 
            collection 要遍历数据
            item 遍历到的元素的变量名
            open/close 指定前面和后面包裹数据时使用的符号
            separator 遍历到的元素的分隔符-->
        <foreach collection="list" item="eid" open="(" close=")" separator=",">
            #{eid}
        </foreach>  
    </delete>
***

合并


这个函数好啊,能将相同的行组合起来,省老事了。
MySQL中group_concat函数
完整的语法如下:
group_concat([DISTINCT] 要连接的字段 [Order BY ASC/DESC 排序字段] [Separator '分隔符'])
用了一次,这是要合并的字段,但是必须分组
GROUP_CONCAT(t_role.info SEPARATOR '/')
这是根据一个表的某个重复的字段来分组
GROUP BY u.`no`
在查询出来之后发现是乱序,可以使用

查询类似于部门与员工表,需要员工的人数的时候直接使用count函数不要考虑的太复杂

不显示为0 的数据
select d.id,d.name,d.Createtime,d.flag,COUNT(1)
from t_depart d
inner join t_staff s on d.id=s.did
GROUP BY d.id
image.png
显示为0的数据
select t_depart.*,
        (select count(1) from t_staff s where s.did = t_depart.id) dcount
   from t_depart;
image.png

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。