在后台开发中必不可少地使用数据库,先将遇到过的场景罗列,便于日后查询使用:

 

删除记录

delete from pi_active_power_config where UU_LIMIT=200;

插入记录

insert into pi_active_power_config (uu_limit, lower_limit, point_id) VALUES (100, 1000, 1);

insert into pi_point (name, device_id) VALUES ("test3", 3);

创建外键

alter table PI_ACTIVE_POWER_CONFIG add constraint FK_ACTIVE_POWER_CONFIG foreign key (POINT_ID)

references PI_POINT (ID) on delete restrict on update restrict;

alter table PI_ACTIVE_POWER_CONFIG add constraint FK_ACTIVE_POWER_CONFIG foreign key (POINT_ID)

references PI_POINT (ID) on delete cascade on update cascade;

删除外键

alter table pi_active_power_config drop FOREIGN key FK_ACTIVE_POWER_CONFIG;

新增一列

ALTER table pi_analog_param_config add COLUMN MESSAGE_SEND tinyint default 0 comment '短信订阅,0为非,1为是' after account;

 

-----------------------------------------查询某个字段是否包含在数据库中-----------------------------------------------

https://blog.csdn.net/TimOut/article/details/74179348

SELECT * FROM information_schema.columns WHERE column_name='column_name';

查询某个表在哪个数据库中:

select * from information_schema.TABLES where TABLE_NAME="safe_start_day"

 

通常java引用代码需要结合MyBatis 使用来操作数据库,关于多参数的传递需要用到列表:

MySql 以及Mybatis 使用问题汇总

 

MySql 以及Mybatis 使用问题汇总

如果只有一个array 或者 list 参数传入,则collection中会默认取list 或者array, 如果有多个则需要用参数注解@Param(“xxx”) 声明参数名称。array 的话取长度为length,   list 的话取长度为size(), 需要注意区分。

<select id="selectNewestImageByDevId" resultType="com.imagewall.entity.ImageInfo">
    select a.* from image_info as a,
    (select stcd, max(b.time) as time from image_info as b group by b.stcd) as b
    where a.stcd=b.stcd and a.time = b.time
    <if test="array != null and array.length>0">
     and b.stcd in
    <foreach collection="array" index="index" item="item" open="(" separator="," close=")">
         #{item}
    </foreach>
    </if>
  </select>

 

还有一个问题是需要注意使用分组group by 之后,并不一定会得到max(b.time) 这条记录的数据,所以为了获取最新时间的数据需要加上条件来判断时间相等。

 

 

传递参数# 和 $ 符号的区别就是,#号会对参数添加单引号,而$则直接拼装SQL:

https://www.cnblogs.com/keyi/p/8509155.html

<select id="pageQueryImagePonitOrderBy" resultType="com.imagewall.entity.ImagePoint">

        SELECT stationid, pno, stcd, stnm, lgtd, ltid, address, createtime, remark, stationName from
        (
        SELECT * FROM `image_point` a LEFT JOIN
        (SELECT stationid as sysStationId, name as stationName from sys_station) b
        on a.stationid=b.sysStationId
        )
        as c

        <where>
            1=1
            <if test="stationIds != null and stationIds.size()>0">
                and c.stationId in
                <foreach collection="stationIds" index="index" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="stcd != null and stcd != ''">
                and c.stcd = #{stcd}
            </if>
            <if test="stnm != null and stnm != ''">
                and c.stnm like "%"#{stnm}"%"
            </if>
        </where>

        <if test="orderByField != null and orderByField != ''">
            ORDER BY ${orderByField}
        </if>

    </select>

相关文章: