常用的mysql查询--mybatis

本文探讨了MyBatis框架中复杂查询的实现方法,包括动态SQL、参数处理及聚合函数应用,通过具体示例展示了如何提高SQL查询效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. <select id="getOrdersGoodByTime" resultMap="BaseResultMap">
        select
        distinct resourceId,goods_name(去掉俩个字段都重复的情况)
        from orders_goods
        <where>
            <choose>    (if,else这样的逻辑)
                <when test="resourseId != null">
                    and resourceId=#{resourseId}
                </when>
                <otherwise>
                    and resourceId!="null"
                </otherwise>
            </choose>
                      (between and 的使用)
            <if test="begin!=null and end!=null">
              and create_time between #{begin} and #{end}
            </if>
        </where>
    </select>

2.<select id="getGoodsListByTime"  resultType="java.util.Map">
        SELECT
        distinct goods_category_sort.*
        from goods,goods_category_sort
        WHERE
        goods.goods_id=goods_category_sort.goods_id and
        <if test="list!=null">
            <foreach collection="list" index="index" item="categoryId" open="goods_category_sort.category_id IN(" separator="," close=")">
                #{categoryId}
            </foreach>
        </if>
        <if test="list!=null">
            <if test="list.size()>0">
                and
            </if>
        </if>
        goods.status=2
        ORDER by goods_category_sort.sort_id asc,goods_category_sort.goods_id asc
    </select>

3.
SELECT
    * 
FROM
    orders os    where DATE_SUB( CURDATE( ), INTERVAL 3 DAY ) <= date( os.create_time )

4.聚合函数后面要加分组

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值