常用的高级sql查询

1.根据主键id数组批量修改

 1    void updateByIdArr(Integer[] idArr);
 2    
 3    <update id="updateByIdArr" parameterType="java.util.Arrays" >
 4     <!--
 5       WARNING - @mbggenerated
 6       This element is automatically generated by MyBatis Generator, do not modify.
 7       This element was generated on Tue Nov 01 11:14:38 CST 2016.
 8     -->
 9     update t_usermanage_adviser
10     set status = 0 
11     where id in 
12     <foreach collection="array" index="index" item="item" open="(" separator="," close=")">
13         #{item}
14     </foreach>
15     
16   </update>

 

 2.批量插入 更新sql

(1). 弊端
如果传入的更新的值为空,会把相应的值更新为空

 1   int batchInsertOrUpdate(List<TShopAttachment> attachments);
 2   
 3   
 4   <insert id="batchInsertOrUpdate" parameterType="java.util.List">
 5     insert into t_shop_attachment
 6     (
 7       enterprise_id, shop_id, type_id, file_path, uploader, updater, file_name, upload_date,
 8       update_date, status
 9     ) values
10     <foreach collection="list" item="item" index="index" separator=",">
11       (
12       #{item.enterprise_id}, #{item.shop_id}, #{item.type_id}, #{item.file_path}, #{item.uploader},
13       #{item.updater}, #{item.file_name}, #{item.upload_date},
14       #{item.update_date}, #{item.status}
15       )
16     </foreach>
17     ON DUPLICATE KEY UPDATE
18     file_path = VALUES(file_path),
19     updater = VALUES(updater),
20     file_name = VALUES(file_name),
21     update_date = VALUES(update_date)
22   </insert>

 

3.关联关系association:1对1关联的三种方法

表结构:

 

order:订单表

order_detail 订单明细表

product 商品表

category 商品分类表

梳理图上的4根关系线:
1、1个用户可以有0个或多个订单:[user表]–>[order表]是1对多(0.n)关系
2、1个有效订单会购买1条或多条商品分录:[order表]–>[order_detail表]是1对多(1.n)关系
3、1条商品分录必定对应一个产品详情:[order_detail表]–>[product表]是1对1关系
4、1个商品分类下可能会有0个或多个商品:[category表]–>[product表]是1对多(0.n)关系
5、所有1对多关系,反过来一定是1对1关系。比如[user表]–>[order表]是1对多(0.n)关系,那么反过来[order表]–>[user表]是1对1关系。每个订单都会有与之对应的唯一用户。
要注意,所有1对1关系,反过来却不一定是1对1关系。比如[order_detail表]–>[product表]是1对1关系,反过来[product表]–>[order_detail表]就不是1对1关系。因为同一个产品可能会在多个不同的订单分录中出现(热门商品大家都愿意买嘛)

[order表]–>[user表]是1对1关系,那么就拿这个来练手。1对1关联关系用于对一个表外键的扩展。
现在要在后台系统中按以下字段显示订单列表。

 

有三种方式可以实现:

一、扩展新建POJO对象,不使用association标签

step1.我们已经有一个与表对应的Order类,但是没有用户名称,用户地址,联系电话这三个字段。现在新建一个类OrderExtend,扩充字段:

 1 public class OrderExtend extends Order{
 2     public OrderExtend() {
 3         super();
 4     }
 5     /*添加用于展示的用户名称,用户地址,联系电话这三个字段*/
 6     String username;
 7     String address;
 8     String cellphone;
 9 
10     /*下面get和set方法*/
11     getter and setter....
12 }

 

step2.创建接口及Xml

1 public interface OrderExtendMapper {
2     //查询单个订单详情,关联查询用户信息
3     public OrderExtend getByOrderno(String orderno) throws Exception;
4     //查询订单列表,关联查询用户信息
5     public List<OrderExtend> getList() throws Exception;
6 }
 1 <mapper namespace="twm.mybatisdemo.mapper.OrderExtendMapper">
 2     <select id="getByOrderno" parameterType="String"
 3         resultType="twm.mybatisdemo.pojo.OrderExtend">
 4         SELECT
 5         `order`.*,`user`.username,`user`.address,`user`.cellphone
 6         FROM `order` ,`user`
 7         WHERE `order`.create_userid=`user`.id AND `order`.create_userid=#{id}
 8     </select>
 9 
10     <select id="getList" resultType="twm.mybatisdemo.pojo.OrderExtend">
11         SELECT
12         `order`.*,`user`.username,`user`.address,`user`.cellphone
13         FROM `order`
14         ,`user`
15         WHERE `order`.create_userid=`user`.id
16     </select>
17 </mapper>
18 

 

step3.调用

1 public static void main(String[] args) throws Exception {
2         SqlSession session = SqlSessionAssist.getSession();
3 
4         OrderExtendMapper ordermapper = session
5                 .getMapper(OrderExtendMapper.class);
6         OrderExtend order = ordermapper.getByOrderno("M201209012578917");
7         System.out.println(order.getOrderno() + "," + order.getUsername() + ","
8                 + order.getAddress() + "," + order.getCellphone());
9     }

 

二、(推荐)用sql联合查询,使用association标签

 1 public class Order{
 2 
 3     User user;
 4 
 5     public User getUser() {
 6         return user;
 7     }
 8     public void setUser(User user) {
 9         this.user = user;
10     }
11     /*下面get和set方法*/
12     getter and setter....
13 }

 

step2.创建映射器接口及配置Xml 
twm.mybatisdemo.mapper包下创建 
OrderMapper.java:

1 public interface OrderMapper {
2     //查询单个订单详情,关联查询用户信息
3     public Order getByOrderno(String orderno) throws Exception;
4     //查询订单列表,关联查询用户信息
5     public List<Order> getList() throws Exception;
6 }

 

OrderMapper.xml:

 1 <mapper namespace="twm.mybatisdemo.mapper.OrderMapper">
 2     <!-- 定义类型映射 -->
 3     <resultMap type="Order" id="OrderMap">
 4         <!-- 订单表属性 -->
 5         <id column="id" property="id" />
 6         <result column="orderno" property="orderno" />
 7         <result column="create_time" property="create_time" />
 8         <result column="create_userid" property="create_userid" />
 9         <!-- 关联的用户信息 -->
10         <!-- association用于关联查询:
11         property指属性,javaType是要映射的对象的类型。 -->
12         <association property="user" javaType="User">
13             <result column="username" property="username" />
14             <result column="address" property="address" />
15             <result column="cellphone" property="cellphone" />
16         </association>
17     </resultMap>
18 
19     <select id="getByOrderno" parameterType="String"
20         resultMap="OrderMap">
21         SELECT
22         `order`.*,`user`.username,`user`.address,`user`.cellphone
23         FROM `order`
24         ,`user`
25         WHERE `order`.create_userid=`user`.id AND
26         `order`.orderno=#{orderno}
27     </select>
28 
29     <select id="getList" resultMap="OrderMap">
30         SELECT
31         `order`.*,`user`.username,`user`.address,`user`.cellphone
32         FROM `order`
33         ,`user`
34         WHERE `order`.create_userid=`user`.id
35     </select>
36 </mapper>

 

step3.调用

 1 public static void main(String[] args) throws Exception {
 2     SqlSession session = SqlSessionAssist.getSession();
 3 
 4     OrderMapper ordermapper = session.getMapper(OrderMapper.class);
 5     Order order = ordermapper.getByOrderno("M201209012578917");
 6     System.out.println(order.getOrderno() + ","
 7             + order.getUser().getUsername() + ","
 8             + order.getUser().getAddress() + ","
 9             + order.getUser().getCellphone());
10 
11 }

 

什么是延迟加载?如果先查询订单信息即可满足业务要求就不会去查询用户,只有当用到用户信息时再查询用户信息。
对用户信息按需去查询就是延迟加载。
比如上面,只有当调用Order中的getUser方法获取关联的user数据时,才会触发数据库查询user表。

mybatis默认没有开启延迟加载,需要在SqlMapConfig.xml中setting配置。
lazyLoadingEnabled:全局性设置懒加载。如果设为‘false’,则所有相关联的都会被初始化加载。允许值有:true | false。默认值:false
aggressiveLazyLoading:当设置为‘true’的时候,懒加载的对象可能被任何懒属性全部加载。否则,每个属性都按需加载。允许值有:true | false。默认值:true

和第二种方式比,其它都不变。只是DAOImplement层有一些变化,XML文件要调整三处:

第一处:新增一个用户查询语句:

1 <!-- 新增一个用户查询语句:getUser -->
2 <select id="getUser" parameterType="int" resultType="User">
3     SELECT
4     `username`,`address`,`cellphone`
5     FROM `user`
6     WHERE `id` =#{_parameter}
7 </select>

 

第二处:把原来resultMap的association标签改为

 1 <association property="user" javaType="User" column="create_userid" select="getUser" /> 

第三处:把getByOrderno和getList查询语句改为普通的select单表查询。

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
 3 <mapper namespace="twm.mybatisdemo.mapper.OrderMapper">
 4     <!-- 定义类型映射 -->
 5     <resultMap type="Order" id="OrderMap">
 6         <!-- 订单表属性 -->
 7         <id column="id" property="id" />
 8         <result column="orderno" property="orderno" />
 9         <result column="create_time" property="create_time" />
10         <result column="create_userid" property="create_userid" />
11         <!-- 关联的用户信息 -->
12         <!-- association用于关联查询: property指属性,javaType是要映射的对象的类型。 -->
13         <association property="user" javaType="User" column="create_userid"
14             select="getUser" />
15     </resultMap>
16 
17 <!-- 新增一个用户查询:getUser。getUser这一段可以删掉,用user对象的查询方法 -->
18 <select id="getUser" parameterType="int" resultType="User">
19     SELECT
20     `username`,`address`,`cellphone`
21     FROM `user`
22     WHERE `id` =#{_parameter}
23 </select>
24 
25     <select id="getByOrderno" parameterType="String" resultMap="OrderMap">
26         SELECT * FROM `order`  WHERE `order`.orderno=#{orderno}
27     </select>
28 
29     <select id="getList" resultMap="OrderMap">
30         SELECT * FROM `order` 
31     </select>
32 </mapper>

 

一切OK了。 
association的几个属性: 
property:指定内部对象属性名 
javaType:内部映射的对象的类型。 
column:要传给select语句的参数,相当于指定外键字段。 
select:指定用户查询语句的ID

 

事实上,大多数业务场景显示的表格,都会用到多个表字段。
如果采用延迟加载,会存在N+1问题。
什么是N+1问题呢?
每一个获取Order内部的User对象,都会进行一次select查询
那么当运行过程中执行Order的getList方法时,SQL首先进行1次查询,查询结果如果有N条订单记录,那么实际在每条订单中显示过程中还要运行一次select用户的查询,共n次。
SQL总共执行了n+1次。相比第二种方法的只进行一次联合查询,这种方式无疑是低效的。
如果业务场景的表格显示字段,并没有跨表,那么可以采用延迟加载方式 

 

 1 <resultMap id="detailMap" type="com.yaoex.usermanage.model.channel.ChannelDetailModel" >
 2     <id column="id" property="id" jdbcType="INTEGER" />
 3     <result column="channel_id" property="channel_id" jdbcType="INTEGER" />
 4     <result column="cust_id" property="cust_id" jdbcType="VARCHAR" />
 5     <result column="cust_group_id" property="cust_group_id" jdbcType="VARCHAR" />
 6     
 7     <association property="custGroup" column="cust_group_id" select="findCustGroup"/>
 8     <association property="cust" column="cust_id" select="findCust"/>
 9   </resultMap>
10 
11 public class ChannelDetailModel extends ChannelDetail {
12     
13     /**
14      * 
15      */
16     private static final long serialVersionUID = -3176604063524770205L;
17 
18     private ChannelCustomer cust;
19     
20     private CustGroupModel custGroup;
21 }
22 
23   <select id="findCustGroup" resultMap="CustGroupModelMap" parameterType="java.lang.String">
24     SELECT id, group_code, group_name FROM t_cust_group WHERE group_code = #{cust_group_id}
25   </select>
26 
27   <select id="findCust" resultMap="custMap" parameterType="java.lang.String">
28     SELECT e.enterprise_id, e.enterprise_name, e.role_type , 
29         case e.role_type 
30             when 1 then 
31                 (select a.param_name 
32                     from t_usermanage_enterprise_type t, t_usermanage_dict a 
33                 where t.type_id = a.param_value and t.enterprise_id = e.enterprise_id) 
34             else '批发企业' 
35         end as customerTypeName
36     FROM t_usermanage_enterprise e 
37     WHERE e.enterprise_id = #{cust_id}
38   </select>

 4.关联关系collection:1对多关联的两种方法

一对多关系 
[order表]–>[order_detail表]是1对多(1.n)关系。1个订单可以由多个订单分录(购买的商品)组成的。1对多关联关系可以理解为一个对象的内部有一个列表。 
在后台系统中我们点击订单列表会进到订单详情页 

有两种方式可以实现:

一、(推荐)用sql联合查询,使用collection标签

sql查询语句是这样的:

1 SELECT
2     `order`.*,`user`.username,`user`.address,`user`.cellphone,
3     `order_detail`.productname,`order_detail`.price,`order_detail`.num
4     FROM `order`
5     ,`user`,`order_detail` 
6     WHERE `order`.create_userid=`user`.id AND
7     `order_detail`.`order_id`=`order`.id AND 
8     `order`.orderno='M201209012578917'

step1.我们已经有一个与表对应的Order类,但是没有分录列表字段。所以在Order类里新增List<OrderDetail> orderdetailList属性:

 1 public class Order {
 2     Integer id;
 3     String orderno;
 4     Integer create_time;
 5     Integer create_userid;
 6     User user;
 7 
 8     /* 添加分录列表 */
 9     List<OrderDetail> orderdetailList;
10 
11     /*下面get和set方法*/
12     getter and setter....
13 }

step2.创建接口及Xml 
在twm.mybatisdemo.mapper包下的OrderMapper.xml和OrderMapper.java的基础上改进。 
OrderMapper.java里添加方法:

 1 //查询单个订单详情,关联查询用户信息以及订单明细 2 public List<Order> getDetailByOrderno(String orderno) throws Exception; 

OrderMapper.xml里添加一个resultMap和一个select:

 1 <resultMap type="Order" id="Contain_DetailList_OrderMap" extends="OrderMap">
 2     <!-- order中订单信息字段及关联的用户字段,和上面的OrderMap映射一样,这里只是扩充订单明细,extends继承上面的即可 -->
 3     <!-- 配置关联订单明细信息 -->
 4     <collection property="orderdetailList" ofType="OrderDetail">
 5         <id column="id" property="id" />
 6         <result column="productname" property="productname" />
 7         <result column="num" property="num" />
 8         <result column="price" property="price" />
 9     </collection>
10 </resultMap>
11 
12 <select id="getDetailByOrderno" parameterType="String" resultMap="Contain_DetailList_OrderMap">
13     SELECT
14     `order`.*,`user`.username,`user`.address,`user`.cellphone,
15     `order_detail`.productname,`order_detail`.price,`order_detail`.num
16     FROM `order`
17     ,`user`,`order_detail` 
18     WHERE `order`.create_userid=`user`.id AND
19     `order_detail`.`order_id`=`order`.id AND 
20     `order`.orderno=#{orderno}
21 </select>

step3.调用

 1 public static void main(String[] args) throws Exception {
 2         SqlSession session = SqlSessionAssist.getSession();
 3 
 4         OrderMapper ordermapper = session.getMapper(OrderMapper.class);
 5         Order order = ordermapper.getDetailByOrderno("M201209012578917");
 6         System.out.println(order.getOrderno() + ","
 7                 + order.getUser().getUsername() + ","
 8                 + order.getUser().getAddress() + ","
 9                 + order.getUser().getCellphone());
10         List<OrderDetail> detailList=order.getOrderdetailList();
11         for (OrderDetail orderDetail : detailList) {
12             System.out.println(orderDetail.getProductname() + ","
13                     + orderDetail.getNum() + ","
14                     + orderDetail.getPrice());
15         }
16     }

结果:

M201209012578917,高天仪,北冥山庄,18911110000 iphone 8 plus,3,5700.0

擦~怎么只有一条明细呢?

看mybatis的日志

1 DEBUG [main] - ==>  Preparing: SELECT 
2   order.*,user.username,user.address,user.cellphone, 
3   order_detail.productname,order_detail.price,order_detail.num 
4   FROM order ,user,order_detail WHERE 
5   order.create_userid=user.id AND 
6   order_detail.order_id=order.id AND order.orderno=?  DEBUG 
7   [main] - ==> Parameters: M201209012578917(String) DEBUG [main] - <== 
8   Total: 2

SQL查询出来确实是2条啊。
上网搜了一下,发现问题出在<id column="id" property="id" />
如果两表联查,主表和明细表的主键都是id的话,明细表的多条只能查询出来第一条。
解决办法:在SQL中加上order_detail.id as detail_id,定义一个别名<id column="detail_id" property="id" />
再把<id column="id" property="id" />改为<id column="detail_id" property="id" />

千万不要试图删了Contain_DetailList_OrderMap里的<id column="id" property="id" />这句。这样没有主键,系统不会distinct去重记录的。

二、(不推荐)不使用sql联合查询,通过collection的延迟加载来实现 
在上面的基础上修改: 
step1.添加select片段,根据订单id,查询所有订单明细记录

1 <select id="SelectOrderDetail" parameterType="int" resultType="OrderDetail">
2     select * from `order_detail` where order_id=#{id}
3 </select>

step2.修改collection 
collection的几个属性: 
property:指定内部对象属性名 
javaType:内部映射的对象的类型。 
column:要传给select语句的参数,相当于指定外键字段。 
select:指定用户查询语句的ID

1 <resultMap type="Order" id="Contain_DetailList_OrderMap" extends="OrderMap">
2     <!-- order中订单信息字段及关联的用户字段,和上面的OrderMap映射一样,这里只是扩充订单明细,extends继承上面的即可 -->
3     <!-- 配置关联订单明细信息 -->
4     <collection property="orderdetailList" select="SelectOrderDetail" column="id" />
5 </resultMap>

step3.修改getDetailByOrderno的select片段,删除联查order_detail相关的部分

1 <select id="getDetailByOrderno" parameterType="String" resultMap="Contain_DetailList_OrderMap">
2     SELECT `order`.*,`user`.username,`user`.address,`user`.cellphone    
3     FROM `order` ,`user`
4     WHERE `order`.create_userid=`user`.id and
5     `order`.orderno=#{orderno}
6 </select>

运行结果:

1 DEBUG [main] - ==>  Preparing: SELECT 
2   order.*,user.username,user.address,user.cellphone FROM order 
3   ,user WHERE order.create_userid=user.id and order.orderno=?  
4   DEBUG [main] - ==> Parameters: M201209012578917(String) DEBUG [main] - 
5   ====>  Preparing: select * from order_detail where order_id=?  DEBUG [main] - ====> Parameters: 1(Integer) DEBUG [main] - <====      Total: 
6   2 DEBUG [main] - <==      Total: 1 
7   M201209012578917,高天仪,北冥山庄,18911110000 iphone 8 plus,3,5700.0 
8   华为G7,2,2700.0

可以看到存在N+1问题,只不过这种场景下N永远是1。 
原来用一个联合查询可以解决的,现在需要用两次查询。

 1   <resultMap id="ChannelDetailResultCustMap" type="com.yaoex.usermanage.model.channel.ChannelProduct" >
 2     <id column="id" property="id" jdbcType="INTEGER" />
 3     <result column="seller_id" property="seller_id" jdbcType="VARCHAR" />
 4     <result column="product_id" property="product_id" jdbcType="VARCHAR" />
 5     <result column="channel_type" property="channel_type" jdbcType="INTEGER" />
 6     <result column="channel_status" property="channel_status" jdbcType="INTEGER" />
 7     <result column="create_user" property="create_user" jdbcType="VARCHAR" />
 8     <result column="create_time" property="create_time" jdbcType="TIMESTAMP" />
 9     <result column="update_user" property="update_user" jdbcType="VARCHAR" />
10     <result column="update_time" property="update_time" jdbcType="TIMESTAMP" />
11     
12     <result column="spu_code" property="spu_code" jdbcType="VARCHAR" />
13     <result column="product_code" property="product_code" jdbcType="VARCHAR" />
14     <result column="product_name" property="product_name" jdbcType="VARCHAR" />
15     <result column="short_name" property="short_name" jdbcType="VARCHAR" />
16     <result column="factory_name" property="factory_name" jdbcType="VARCHAR" />
17     <result column="spec" property="spec" jdbcType="VARCHAR" />
18     <result column="drugform" property="drugform" jdbcType="VARCHAR" />
19       <!--javatype指定的是user对象的属性的类型(例如id,canBuyCust),而oftype指定的是映射到list集合属性中pojo的类型(本例指的是ChannelCustomer类型)-->
20     <collection property="canBuyCust" ofType="com.yaoex.usermanage.model.channel.ChannelCustomer" column="{id=id,canBuyFlag=can_buy}" select="findCanBuyCustWithCust"/>
21     <collection property="canNotBuyCust" ofType="com.yaoex.usermanage.model.channel.ChannelCustomer" column="{id=id,canBuyFlag=cannot_buy}" select="findCanBuyCustWithCust"/>
22   </resultMap>

 

 1 public class ChannelProduct extends Channel {
 2     /**
 3      * 
 4      */
 5     private static final long serialVersionUID = 1595115735063349179L;
 6 
 7     private String spu_code;
 8     
 9     private String product_code;
10     
11     private String product_name;
12     
13     private String short_name;
14     
15     private String factory_name;
16     
17     private String spec;
18     
19     private String drugform;
20     
21     private Integer canNotBuyCustNum;
22     
23     private List<ChannelCustomer> canBuyCust;
24     
25     private List<ChannelCustomer> canNotBuyCust;
26 }
 1   <!-- 按客户添加的渠道    查看客户-->
 2   <select id="findCanBuyCustWithCust" resultMap="customerResultMap" parameterType="map">
 3     SELECT t.cust_id, t.cust_group_id, e.enterprise_id, e.enterprise_name, e.role_type,
 4         case e.role_type 
 5             when 1 then 
 6                 (select a.param_name 
 7                     from t_usermanage_enterprise_type t, t_usermanage_dict a 
 8                 where t.type_id = a.param_value and t.enterprise_id = e.enterprise_id) 
 9             else '批发企业' 
10         end as customerTypeName 
11     FROM t_channel_detail t left join t_usermanage_enterprise e on e.enterprise_id = t.cust_id
12 
13     WHERE t.channel_id = #{id} and t.can_buy_flag = #{canBuyFlag} and t.is_condition = 0 
14 
15   </select>

 5、批量更新sql:

(1)根据一个字段批量更新:List<String> channelIdSet

1         Map<String,Object> params = new HashMap<>();
2         params.put("channelIdSet", channelIdSet);
3         this.baseDao.update("com.yaoex.usermanage.persistence.interfaces.channel.ChannelMapper.batchDeleteChannel", params);
 1     <update id="batchDeleteChannel" parameterType="map">
 2         update t_channel
 3         <trim prefix="set" suffixOverrides=",">
 4             <trim prefix="is_delete = case" suffix="end,">
 5                 <foreach collection="channelIdSet" item="item">
 6                     when id=#{item} then 1
 7                 </foreach>
 8             </trim>
 9         </trim>
10         <where>
11             and id in
12             <foreach collection="channelIdSet" item="item" open="(" close=")" separator=",">
13                 #{item}
14             </foreach>
15         </where>
16     </update>

 (2)根据多个字段同时更新:Set<ProductUniqueBean> productUniqueBeans

1     @Override
2     public void batchDeleteChannelApply(Set<ProductUniqueBean> productUniqueBeans) {
3         Map<String,Object> params = new HashMap<>();
4         params.put("productUniqueBeans", productUniqueBeans);
5         this.baseDao.update("com.yaoex.usermanage.persistence.interfaces.channelapply.ChannelApplyMapper.batchDeleteChannelApply", params);
6     }
 1     <update id="batchDeleteChannelApply" parameterType="map">
 2         update t_channel_apply
 3         <trim prefix="set" suffixOverrides=",">
 4             <trim prefix="is_delete = case" suffix="end,">
 5                 <foreach collection="productUniqueBeans" item="item">
 6                     when spu_code=#{item.spuCode} and seller_code=#{item.sellerCode} then 1
 7                 </foreach>
 8             </trim>
 9         </trim>
10         <where>
11             and (spu_code,seller_code) in
12             <foreach collection="productUniqueBeans" item="item" open="(" separator="," close=")">
13                 (#{item.spuCode},#{item.sellerCode})
14             </foreach>
15         </where>
16     </update>

 

 

6、统计数量方法

1 SELECT IFNULL(SUM(if(d.role_type = 1,2,0)),0) roleType from t_usermanage_enterprise_dft d ;
2 
3 SELECT IFNULL(SUM(d.role_type = 1),0) roleType from t_usermanage_enterprise_dft d;
4 
5 SELECT COUNT(*) from t_usermanage_enterprise_dft d WHERE d.role_type = 1;

 

7、时间范围搜索

(1)

1 AND STR_TO_DATE(#{now},"%Y-%m-%d") >= STR_TO_DATE(t2.start_time,"%Y-%m-%d")
2 AND STR_TO_DATE(t2.end_time,"%Y-%m-%d") >= STR_TO_DATE(#{now},"%Y-%m-%d")

 (2)

1       <if test="startTime != null and startTime != ''">
2             <![CDATA[  and updateTime >=  #{startTime} ]]>
3       </if>
4       <if test="endTime != null and endTime != ''">
5             <![CDATA[  and updateTime <=  #{endTime} ]]>
6       </if>

 

posted @ 2018-10-10 18:44  小小蔡芽  阅读(632)  评论(0编辑  收藏  举报