陈世涛:SQL语句
增
插入数据
保证数据列数正确
JdbcTemplate template = new JdbcTemplate(CnetralDBJDBC.getDataSource());
String sql = "insert into user1 values(?,?,null,?,?,null,null,?)";
int i = template.update(sql, staffNo, password, factory, factoryId, type);
删
删除某个表中所有数据
JdbcTemplate template = new JdbcTemplate(CnetralDBJDBC.getDataSource());
String sql="TRUNCATE TABLE protectactionshowTime";
template.update(sql);
删除满足条件的列
JdbcTemplate templateCentral = new JdbcTemplate(CnetralDBJDBC.getDataSource());
// 删除所有未完结的数据(包括上月和本月的)
templateCentral.update("delete from singleactioncountall where flag = 0");
改
更改某列数据
JdbcTemplate template = new JdbcTemplate(CnetralDBJDBC.getDataSource());
// 写入保护种类名称,及对应的条数
String sql ="update ProTypeData set ProTypeCount = ? where ProTypeName = ? and FactoryId = ? and ProTypeClass = 0";
template.update(sql,singleCount,s,FId);
更改多个
String sql8 = "update protectactionsave set " + typecount + " = ?," + typetime + " = ?,最后id = ?,总次数 = ?,总时间 = ?,更新时间 = ? where id =?";
templateCentral.update(sql8, savecount, savetime, data.getBH_ID(), totalcount, totaltime, now, data3.getId());
查
查询所有
JdbcTemplate template = new JdbcTemplate(CnetralDBJDBC.getDataSource());
String sql = "select * from ProType where Id = ?";
ProType proType1 = template.queryForObject(sql, new BeanPropertyRowMapper<ProType>(ProType.class),a);
排序倒序查
JdbcTemplate template = new JdbcTemplate(CnetralDBJDBC.getDataSource());
String sql = "select * from ProTypeData where FactoryId = ? and ProTypeClass = 0 order by ProTypeCount DESC";
List<ProTypeData> list = template.query(sql, new BeanPropertyRowMapper<ProTypeData>(ProTypeData.class),FId);
like查询
查找关键字中不包含任何一个中括号里的字的查询
String sql = "select count(*) from xmc_bhtc1 where 查询日期 between ? and ? and 保护名称 not like '%[合漏观源停轻]%'";
int count = template.queryForObject(sql,Integer.class, StarTime,EndTime);
相似查询
JdbcTemplate template = new JdbcTemplate(CoalFeederTimeJDBC.getDataSource());
String sql="select * from CoalFeederTime where factory=? and position like '%产品仓%' order by position";
List<CoalFeederTime> CoalFeederTimeData=template.query(sql,new BeanPropertyRowMapper<CoalFeederTime>(CoalFeederTime.class),factoryname);
按月日期查询
查找5月份的数据
JdbcTemplate template = new JdbcTemplate(CnetralDBJDBC.getDataSource());
String sql = "select * from protectactioncountorderall where 厂号 = ? and CONVERT(varchar(7),数据时间, 120 ) = '2020-05-20' ";
list = template.query(sql, new BeanPropertyRowMapper<countOrder>(countOrder.class), FId);
查找第一个
JdbcTemplate template = new JdbcTemplate(CnetralDBJDBC.getDataSource());
String sql = "select top 1 * from user1 where FactoryID = ?";
User user = template.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), Fid);
查次数-日期条件
具体月份查询
JdbcTemplate template = new JdbcTemplate(CnetralDBJDBC.getDataSource());
String sql1 = "select count(*) from singleactioncountall where factoryid = ? and count1 >= 5 and CONVERT(varchar(7),datatime, 120 ) = '2020-05-20' ";
int count1 = template.queryForObject(sql1, Integer.class, fid);
本月查询
JdbcTemplate template = new JdbcTemplate(CnetralDBJDBC.getDataSource());
String sql1 = "select count(*) from xmc_bhtc1 where DATEDIFF(month,查询日期,GETDATE()) = 0 and 撤销时长 > 30 and 保护名称 not like '%[合漏观源停轻]%'";
int count = template.queryForObject(sql1, Integer.class);
顺序逐条查询
JdbcTemplate template = new JdbcTemplate(CnetralDBJDBC.getDataSource());
String sql = "select top 1 * from xmc_bhdz1 where BH_ID not in (select top " + (i - 1) + " BH_ID from xmc_bhdz1 where BH_ID > ? and DATEDIFF(month,查询日期,GETDATE())= 0 and 保护名称 not like '%[合漏观源停轻]%' ) and BH_ID > ? and DATEDIFF(month,查询日期,GETDATE())= 0 and 保护名称 not like '%[合漏观源停轻]%' ";
xmc_bhdz data = template.queryForObject(sql, new BeanPropertyRowMapper<xmc_bhdz>(xmc_bhdz.class), startid, startid);
逆序查询
查询id最大的数据
JdbcTemplate templateCentral = new JdbcTemplate(CnetralDBJDBC.getDataSource());
String sql = "select top 1 * from (select top " + i + " * from protectactionsave where DATEDIFF(month,更新时间,GETDATE())= 0 and 厂号 = ? order by 最后id desc )A order by 最后id ";
cn.itcast.domain.protectsave data = templateCentral.queryForObject(sql, new BeanPropertyRowMapper<cn.itcast.domain.protectsave>(cn.itcast.domain.protectsave.class),fid);
逆序查询
String sq2 = "select top 1 * from (select top 1 * from WarnTimeSave where flag = 0 order by recordtime DESC )A order by recordtime ";//倒序
WarnTimeSave data6 = template.queryForObject(sq2, new BeanPropertyRowMapper<WarnTimeSave>(WarnTimeSave.class));
复制
sqlserver复制表数据到另一个表
SQL Server中,如果目标表存在:
insert into 目标表 select * from 原表;
SQL Server中,,如果目标表不存在:
select * into 目标表 from 原表;
Oracle中,如果目标表存在:
insert into 目标表 select * from 原表;
commit;
Oracle中,如果目标表不存在:
create table 目标表 as select * from 原表;