sql的基本用法

sql片段

<sql id="sqlid"> res_type_id,res_type </sql>
<select id="selectbyId" resultType="com.property.vo.PubResTypeVO"> select <include refid="sqlid"/> from pub_res_type </select>



单个模糊查询
SELECT * FROM `edu_teacher` WHERE `name` like CONCAT('%',#{name},'%');

去重
SELECT DISTINCT 字段 FROM TABLE


mysql的+号的使用: 只有一个功能 就是运算符 “123”+12 将123转为数值型在相加 “join”+123 讲join转为0在相加 那如果一方为null 那结果肯定为null


concat(字段1,字段2)连接
  SELECT CONCAT(`name`,sex)  连接  FROM `com_housekeeper`;


条件查询:
SELECT DISTINCT 字段 FROM TABLE  where 筛选条件
  between   100  and   150
SELECT  字段 FROM TABLE  where 条件字段  between   100  and   150

in:判断某个字段的值是否属于in列表

条件字段  in(值1,值2,值3)

is null :查询字段为null

SELECT  字段 FROM TABLE  where 条件字段 is  null

is  not null :查询字段不为null
SELECT  字段 FROM TABLE  where 条件字段 is  not null

order by 排序列表 asc|desc 默认是asc 一般放在查询语句的最后面

select 查询列表 from table where 筛选条件 order by 排序列表 asc|desc

常见的函数:
1.单行函数
concat,ifnull,length
upper(小写) lower(大写)

2.分组函数(都忽悠null值)
功能:做统计使用,又称为统计函数,聚合函数,组函数

sum求和 avg平均值 max最大值 min最小值 count计算个数

sum求和 avg平均值 一般处理数值型
max最大值 min最小值 count计算个数 处理任何类型  

常见的数据类型:

时间数据类型
Mysql中用来存储日期的数据类型有三种:Date、Datetime、Timestamp。
date数据类型:用来存储没有时间的日期。
datetime类型:日期和时间的组合。
timestamp类型:时间戳。范围是'1970-01-01 00:00:00'到2037年。



插入之后获取id信息

<!--keyProperty:pojo的id ,keyColumn:数据库的id order是先做还是后做 AFTER插入之后 -->
 <insert id="save"  parameterType="com.jiu.dao.EduTeacher" >
<selectKey keyProperty="id" keyColumn="id" order="AFTER">
SELECT LAST_INSERT_ID();
</selectKey>
INSERT INTO `table` (`name`,intro) VALUES (#{name},#{intro});
</insert>

ognl表达式:
@Data
public class EduTeacher implements Serializable {
private String name;

}
public class QueryVo {
private EduTeacher eduTeacher;
}
<select id="fidAllQueryVo" resultType="com.jiu.dao.EduTeacher" parameterType="com.jiu.dao.QueryVo">
SELECT * FROM `edu_teacher` WHERE `name` like CONCAT('%',#{eduTeacher.name},'%');
</select>



resultmap的执行效率比resulType要差


条件查询
<select id="fidAllCondition" resultType="com.jiu.dao.EduTeacher" parameterType="com.jiu.dao.EduTeacher">
SELECT * FROM `edu_teacher`
<where>
<if test="id != null and id!= ''">
            and id=#{id}
</if>
</where>

</select>

添加操作
<insert id="insertNotice" parameterType="SysNotice">
insert into sys_notice (
<if test="noticeTitle != null and noticeTitle != '' ">notice_title,</if>
<if test="noticeType != null and noticeType != '' ">notice_type,</if>
<if test="noticeContent != null and noticeContent != '' ">notice_content,</if>
<if test="status != null and status != '' ">status,</if>
<if test="remark != null and remark != ''">remark,</if>
<if test="image != null and image != ''">image,</if>
<if test="createBy != null and createBy != ''">create_by,</if>
create_time
)values(
<if test="noticeTitle != null and noticeTitle != ''">#{noticeTitle},</if>
<if test="noticeType != null and noticeType != ''">#{noticeType},</if>
<if test="noticeContent != null and noticeContent != ''">#{noticeContent},</if>
<if test="status != null and status != ''">#{status},</if>
<if test="remark != null and remark != ''">#{remark},</if>
<if test="image != null and image != ''">#{mage},</if>
<if test="createBy != null and createBy != ''">#{createBy},</if>
sysdate()
)


截取日期 记得加''符合
   DATE_FORMAT(vsleep,'%H:%i') vsleep,
    DATE_FORMAT(create_time, '%Y-%c-%d %H:%i:%S')


给所有数据加序号
 row_number() over(partition by 分组列 order by 排序列 desc)
 


posted @ 2021-03-29 00:15  阿布v  阅读(270)  评论(0)    收藏  举报