该博客内容涉及SQL查询的自定义排序ORDER BY FIELD方法,以及数据库的增删改查操作,包括条件过滤、索引创建、数据备份等。还提到了Mapper文件中resultMap的使用,用于映射复杂对象关系。
摘要由CSDN通过智能技术生成
自定义排序-ORDER BY FIELD
<selectid="pageByQuery"parameterType="com.wekj.ministore.model.query.MiniProductPageQuery"resultType="com.wekj.ministore.dao.entity.MinistoreProduct">
select *
from ministore_product as a
where deleted = 0
<iftest="query.cliqueId != null and query.cliqueId != ''">
AND clique_id = #{query.cliqueId}
</if><iftest="query.status != null and query.status != ''">
AND status = #{query.status}
</if><iftest="query.keyword != null and query.keyword != ''">
AND ( title like concat ('%', #{query.keyword}, '%')
or out_product_id = #{query.keyword}
<iftest="query.keywordNumber != null">
or product_id = #{query.keywordNumber}
</if>
)
</if><iftest="query.productIds != null and query.productIds.size() > 0 ">
and product_id in
<foreachcollection="query.productIds"item="productId"open="("close=")"separator=",">
#{productId}
</foreach></if><choose><whentest="query.productIds != null and query.productIds.size() > 0 and query.productIdsSort != null and query.productIdsSort == true">
ORDER BY FIELD ( a.product_id,
<foreachcollection="query.productIds"item="item"index="index"separator=",">
${item}
</foreach>
)
</when><whentest="query.sort != null">
ORDER BY
<foreachcollection="query.sort"item="item"index="index"separator=",">
${item.field} ${item.type.code}
</foreach></when><otherwise>
ORDER BY
ID
DESC
</otherwise></choose>
LIMIT #{query.startRow} ,#{query.pageSize}
</select>
查询指定一行排序到第一行
select*from 库名.表名 ORDERBY id =7desclimit0,5;select*from 库名.表名 ORDERBY id <>7limit5,5;select*from 库名.表名 ORDERBY id in(7,8,9)desclimit0,5;select*from 库名.表名 ORDERBY id notin(7,8,9)limit5,5;
DDL相关SQL
-- alter表altertable 库名.表名 addcolumn org_tag_type intDEFAULT'-1'comment'签'AFTER id;altertable 库名.表名 modifycolumn org_tag_type intDEFAULT'-1'comment'签';altertable 库名.表名 dropcolumn certificate_source;-- 直接删除整张表DROPTABLEIFEXISTS 库名.表名;-- 修改 updateupdate 库名.表名 set login_host ='h'where code ='8';-- 自定义排序ORDER BY FIELDSELECT*from 库名.表名 WHERE`code`IN(500103,500105,500108,500106,500107,500104)ORDERBY FIELD (code,500103,500105,500108,500106,500107,500104);-- 指定数据在第一行select*from 库名.表名 ORDERBY id =7desclimit0,5;select*from 库名.表名 ORDERBY id <>7limit5,5;select*from 库名.表名 ORDERBY id in(7,8,9)desclimit0,5;select*from 库名.表名 ORDERBY id notin(7,8,9)limit5,5;-- 查询逗号分割的字符串数组select org_service_category, out_org_id, full_name, social_credit_code, settle_type from 库名.表名 where settle_type =1and(find_in_set('project',`org_service_category`)or find_in_set('service',`org_service_category`));
索引相关
-- 加索引createindex 索引名一般前缀idx_ on 表名 (列名);
备份表
INSERTINTO paas_XXX_bak (id,status, created_at, updated_at, ext_fields)select id,status,
created_at,
updated_at,
ext_fields
from paas_XXX
where XXX ='~'and XXX ='~'onduplicatekeyupdate id =VALUES(id),status=VALUES(status),
created_at =VALUES(created_at),
updated_at =VALUES(updated_at),
ext_fields =VALUES(ext_fields);