常用的sql语句

from_unixtime和unix_timestamp函数使用

 

 

    <insert id="offerClickStatistics">
    insert into clickstatistics (
        insuranceClickRate,
        offerClickRate,
        busDate,
        createTime,
        updatetime) values (0,1,FROM_UNIXTIME(unix_timestamp(now()), '%Y%m%d'),unix_timestamp(now()),unix_timestamp(now()))
  </insert>

<!--从现在起七天之前的数据 FROM_UNIXTIME( unix_timestamp( now()) - (86400 * 7)为现在的时间戳减去7天时间的时间戳,使用from_unixtime函数转成类似20210911这样的Int类型的数据 -->
    <select id="getCompareByData" resultType="com.wanshun.config.cardatamodel.entity.CarInsuranceDayDo">
    SELECT
    *
    FROM
    carinsuranceday
    where
         busDate <= FROM_UNIXTIME( unix_timestamp( now()) - (86400 * 7), '%Y%m%d' )
  </select>

时间戳转时间格式:       FROM_UNIXTIME( 10位数的时间戳, '%Y%m%d' )

时间转时间戳格式:     unix_timestamp( now())或unix_timestamp(date时间)

  <select id="getOrderStatistics" resultType="com.wanshun.order.dto.output.OrderStatisticsDto">
        SELECT
        count( CASE WHEN orderStatus = 5 THEN orderId END ) AS complainOrderCount,
        COALESCE ( sum( CASE WHEN orderStatus = 5 THEN totalPremium END ), 0 ) AS finishOrderCount,
        FROM_UNIXTIME( unix_timestamp( now()), '%Y%m%d' ) AS busDate
        FROM
        autoinsurance
        <where>
            <if test="currentTime != null">
                and FROM_UNIXTIME( updateTime, '%Y%m%d' ) = FROM_UNIXTIME( #{currentTime}, '%Y%m%d' )
            </if>
            <if test="currentTime == null">
                and FROM_UNIXTIME( updateTime, '%Y%m%d' ) = FROM_UNIXTIME( unix_timestamp( now()), '%Y%m%d' )
            </if>
        </where>
    </select>

    <select id="getOrderStatisticsSum" resultType="com.wanshun.order.dto.output.OrderStatisticsDto">
        SELECT
        count( CASE WHEN orderStatus = 5 THEN orderId END ) AS complainOrderCountSum,
        COALESCE ( sum( CASE WHEN orderStatus = 5 THEN totalPremium END ), 0 ) AS finishOrderCountSum,
        FROM_UNIXTIME( unix_timestamp( now()), '%Y%m%d' ) AS busDate
        FROM
        autoinsurance
    </select>

    <select id="getOrderStatisticsDay" resultType="com.wanshun.order.dto.output.OrderStatisticsDto">
        SELECT
        count( CASE WHEN orderStatus = 5 THEN orderId END ) AS complainOrderCount,
        COALESCE ( sum( CASE WHEN orderStatus = 5 THEN totalPremium END ), 0 ) AS finishOrderCount,
        FROM_UNIXTIME( updateTime, '%Y%m%d' ) AS busDate
        FROM
        autoinsurance
        where FROM_UNIXTIME( updateTime, '%Y%m%d' ) &lt;= #{endDate}
        <if test="startDate != null">
            and FROM_UNIXTIME( updateTime, '%Y%m%d' ) &gt;#{startDate}
        </if>
        GROUP BY
        busDate
        ORDER BY
        busDate DESC
    </select>

foreach标签使用和like模糊查询使用

 <select id="getOrderList" resultType="com.wanshun.order.cardatamodel.entity.AutoInsuranceDo"
            parameterType="com.wanshun.order.dto.input.AutoInsuranceQueryDto">
        select
       *
        from autoinsurance
        <where>
            <if test="carIds != null and carIds.size() > 0">
                and carId in
                <foreach collection="carIds" index="index" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="autoInsuranceQueryDto.orderId != null">
                and orderId = #{autoInsuranceQueryDto.orderId}
            </if>
            <if test="autoInsuranceQueryDto.licenseNumber != null">
                and insuredInfo LIKE concat('%',#{autoInsuranceQueryDto.licenseNumber},'%')
            </if>
            <if test="autoInsuranceQueryDto.supplierId != null">
                and suppliersId = #{autoInsuranceQueryDto.supplierId}
            </if>
            <if test="autoInsuranceQueryDto.orderStatus != null">
                and orderStatus = #{autoInsuranceQueryDto.orderStatus}
            </if>
            <if test="autoInsuranceQueryDto.startTime != null">
                and createTime &gt;= #{autoInsuranceQueryDto.startTime}
            </if>
            <if test="autoInsuranceQueryDto.endTime != null">
                and createTime &lt;= #{autoInsuranceQueryDto.endTime}
            </if>
        </where>
        ORDER BY createTime DESC
    </select>

<!--批量新增-->
  <insert id="insertBatch" >
        insert into appupgradegrayleveltest
        (fileId, phone,userId, appUpgradeId, createTime, createUserId)
        values
        <foreach collection="list" item="obj" separator="," >
            (#{obj.fileId},#{obj.phone},#{obj.userId},#{obj.appUpgradeId},#{obj.createTime},#{obj.createUserId})
        </foreach>
    </insert>


<!--多个foreach使用-->
    <!--投诉条件查询-->
    <select id="getCancelReasonList" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from complainreason
        <include refid="BASE_INFORMATION_ORDERBY"/>

    </select>

    <!--条件查询-条件-->
    <sql id="BASE_INFORMATION_ORDERBY">
        <where>1=1
            <if test="bean.type != null">
                and type = #{bean.type}
            </if>
            <if test="bean.orderId != null">
                and orderId = #{bean.orderId}
            </if>
            <if test="bean.labelId != null">
                and labelId = #{bean.labelId}
            </if>
            <if test="bean.orderStatus != null">
                and orderStatus = #{bean.orderStatus}
            </if>
            <if test="bean.createTime != null">
                and createTime &gt;= #{bean.createTime} and createTime &lt;= #{createEndTime}
            </if>

            <if test="passengerId != null and  passengerId.size > 0">
                and passengerId in
                <foreach item="item" index="index" collection="passengerId"
                         open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>

            <if test="driverId != null and driverId.size > 0">
                and driverId in
                <foreach item="item" index="index" collection="driverId"
                         open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>

            order by createTime desc
        </where>
    </sql>

<!--两个表下的查询,一个查表的全部字段,一个查部分字段-->
<select id="queryDriverCityConfig" resultType="com.wanshun.config.cardatamodel.po.config.DriverCityConfigDo">
        select d.*, c.cityCode from drivercityconfig d, cityconfig c
        where d.id = c.typeId and c.type = 4 and c.cityCode in
        <foreach collection="cityCodes" item="cityCode" open="(" separator="," close=")">
            #{cityCode}
        </foreach>
    </select>

group_concat函数使用

    <select id="queryDriverCityConfigs" parameterType="com.wanshun.config.rpcao.config.RpcDriverCityConfigListAo"
            resultType="com.wanshun.config.rpcvo.config.RpcDriverCityConfigVo">
        select d.id, d.name, d.state, group_concat(c.cityCode) cityCodeStr, d.updateUser, d.updateTime from drivercityconfig d, cityConfig c
        where c.type = 4 and d.id = c.typeId
        <if test="name != null and name != ''">
            and d.name like CONCAT(#{name}, '%')
        </if>
        <if test="state != null">
            and d.state = #{state}
        </if>
        <if test="id != null">
            and d.id = #{id}
        </if>
        group by c.typeId order by d.id = 1 desc, d.updateTime desc
        <if test="start != null and pageSize != null">
            limit #{start}, #{pageSize}
        </if>
    </select>

 

mapper中一个sql id写俩sql语句


    <!--定时任务-->
    <update id="changeInformationStatus" parameterType="Long">
        <!-- 待生效 变成 生效中 -->
        update information
        set putawayStatus = 2,effectStatus =3 , updateTime = #{updateTime}
        where effectStatus = 2 and putawayStatus = 2 and effectStartTime &lt;= #{updateTime};

        <!-- 生效中 变成 上架已失效 -->
        update information
        set putawayStatus = 2, effectStatus = 4, updateTime = #{updateTime}
        where effectStatus = 3 and putawayStatus =2 and effectEndTime &lt; #{updateTime};
    </update>

 trim标签使用

    <insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.wanshun.drivingservice.drivingmodel.po.CommentLabel" useGeneratedKeys="true">
        insert into commentlabel
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="labelName != null">
                labelName,
            </if>
            <if test="labelCode != null">
                labelCode,
            </if>
            <if test="enable != null">
                enable,
            </if>
            <if test="labelType != null">
                labelType,
            </if>
            <if test="labelLevel != null">
                labelLevel,
            </if>
            <if test="createTime != null">
                createTime,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="labelName != null">
                #{labelName,jdbcType=VARCHAR},
            </if>
            <if test="labelCode != null">
                #{labelCode,jdbcType=INTEGER},
            </if>
            <if test="enable != null">
                #{enable,jdbcType=TINYINT},
            </if>
            <if test="labelType != null">
                #{labelType,jdbcType=TINYINT},
            </if>
            <if test="labelLevel != null">
                #{labelLevel,jdbcType=TINYINT},
            </if>
            <if test="createTime != null">
                now() ,
            </if>
        </trim>
    </insert>

sql标签使用

<sql id="BASE_WHERE_SQL">
        <if test="orderId != null and orderId != '' ">
            and orderId = #{orderId}
        </if>
        <if test="carOwnerPhone != null and carOwnerPhone != '' ">
            and carOwnerPhone = #{carOwnerPhone}
        </if>
        <if test="driverPhone != null and driverPhone !=  '' ">
            and driverPhone = #{driverPhone}
        </if>
        <if test="orderStatus != null and orderStatus != 0">
            and orderStatus = #{orderStatus}
        </if>
        <if test="complainReason != null and complainReason != '' ">
            and complainReason = #{complainReason}
        </if>
        <if test="driverNum != null and driverNum != '' ">
            and driverNum = #{driverNum}
        </if>
        <if test="remarkStarLevel != null and remarkStarLevel!=0">
            and remarkStarLevel = #{remarkStarLevel}
        </if>
        <if test="cancelReason != null and cancelReason != '' ">
            and cancelReason = #{cancelReason}
        </if>
        <if test="carOwnerNickName != null and  carOwnerNickName != '' ">
            and carOwnerNickName = #{carOwnerNickName}
        </if>
        <if test="complainTime != null   and   endTime != null  ">
            and complainTime between #{complainTime} and #{endTime}
        </if>
        <if test=" remarkTime != null and remarkTimes != null  ">
            and remarkTime between #{remarkTime} and #{remarkTimes}
        </if>
        <if test="cancelOrderTime != null  and endTime != null ">
            and cancelOrderTime between #{cancelOrderTime} and #{endTime}
        </if>
        <if test="orderTime != null  and endOrderTime != null  ">
            and orderTime between #{orderTime} and #{endOrderTime}
        </if>
        <if test="payTime != null  and endPayTime != null  ">
            and payTime between #{payTime} and #{endPayTime}
        </if>
        <if test="payCashier != null   ">
            and payCashier = #{payCashier}
        </if>
        ORDER BY createTime DESC
    </sql> 
<sql id="CONTAIN_PAYSTATUS_SQL">
        <where>
            <if test="orderId != null and orderId !=  '' ">
                and orderId = #{orderId}
            </if>
            <if test="carOwnerPhone != null and carOwnerPhone != '' ">
                and carOwnerPhone = #{carOwnerPhone}
            </if>
            <if test="driverPhone != null and driverPhone != '' ">
                and driverPhone = #{driverPhone}
            </if>
            <if test="orderStatus != null and orderStatus != 0">
                and orderStatus = #{orderStatus}
            </if>
            <if test="complainReason != null and complainReason != '' ">
                and complainReason = #{complainReason}
            </if>
            <if test="driverNum != null and driverNum != '' ">
                and driverNum = #{driverNum}
            </if>
            <if test="remarkStarLevel != null and remarkStarLevel!=0">
                and remarkStarLevel = #{remarkStarLevel}
            </if>
            <if test="cancelReason != null and cancelReason != '' ">
                and cancelReason = #{cancelReason}
            </if>
            <if test="payStatus != null and payStatus != '' ">
                and payStatus = #{payStatus}
            </if>
            <if test="carOwnerNickName != null and carOwnerNickName != '' ">
                and carOwnerNickName = #{carOwnerNickName}
            </if>
            <if test="complainTime != null and endTime != null  ">
                and complainTime between #{complainTime} and #{endTime}
            </if>
            <if test="remarkTime != null    and remarkTimes != null  ">
                and remarkTime between #{remarkTime} and #{remarkTimes}
            </if>
            <if test="cancelOrderTime != null  and endTime != null ">
                and cancelOrderTime between #{cancelOrderTime} and #{endTime}
            </if>
            <if test="orderTime != null    and endOrderTime != null ">
                and orderTime between #{orderTime} and #{endOrderTime}
            </if>
            <if test=" payTime != null  and endPayTime != null   ">
                and payTime between #{payTime} and #{endPayTime}
            </if>
        </where>
        ORDER BY createTime DESC
    </sql>

    <sql id="Base_Column_List">
        bookingId,bookingTime,orderId,drivingOrderId,tradeOrderId,orderType,payStatus,startAddress,startLon,
        startLat,getUpAddress,getUpLon,getUpLat,getUpTime,cancelOrderAddress,cancelOrderLon,cancelOrderLat,getOffAddress,getOffLon,
        getOffLat,getOffTime,destAddress,destLon,destLat,estimateFeeDetail,lastFeeDetail,carOwnerNickName,carOwnerPhone,driverPhone,
        driverNum,orderBeginCityName,orderTime,acceptTime,estimatedTimeArrival,orderStatus,waitingTime,waitingFee,tip,orderDistance,
        orderDuration,orderMoney,realPayMoney,payTime,cancelOrderTime,cancelReason,isDutyCancelOrder,cancelReasonDetail,complainReason,
        complainTime,replenishComplainContent,remarkContent,remarkTime,remarkStarLevel,createTime,updateTime,userId,payChannel,payCashier
    </sql>

<select id="getDrivingList" parameterType="com.wanshun.drivingservice.drivingmodel.po.DrivingOrder"
            resultType="com.wanshun.drivingservice.drivingmodel.po.DrivingOrder">
        select
        <include refid="Base_Column_List"/>
        from drivingorder
        <include refid="CONTAIN_DRIVING_SQL"/>
    </select>

推荐博客 

mysql数据库 count() 函数和 sum() 函数用法和区别_white_ice的博客-CSDN博客_mysql sum函数 

MySQL中IF()、IFNULL()、NULLIF()、ISNULL()函数的使用_pan_junbiao的博客-CSDN博客_ifnull mysql COALESCE函数_IT云清-CSDN博客

 小白一枚,单纯记录为自己看

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值