项目源码网址 https://github.com/studychen/SeeNewsServer 欢迎star
建表语句
CREATE TABLE `rotation` (
`id` int(11) NOT NULL,
`image_urls` text,
`title` varchar(100) DEFAULT NULL,
`publish_date` date NOT NULL,
`read_times` int(11) NOT NULL,
`source` varchar(50) DEFAULT NULL,
`body` longtext,
UNIQUE KEY `id_UNIQUE` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
replace 函数
项目中经常需要把某个字段的一些值替换为另一个值
update master set body=REPLACE(body, 'http//dp.xidian.edu.cn',
'912720f605b84070e223d0dab690a114') where id =3337
这段sql语句的意思是把表master中的body字段里的http//dp.xidian.edu.cn
替换为912720f605b84070e223d0dab690a114
后面还加上了限定条件where id =3337
修改字段的类型
项目组本来source字段(新闻来源字段)是varchar(20),插入数据的时候
发现有些数据长度 > varchar(20),需要变长为varchar(50)
alter table notific MODIFY source varchar(50);
这段sql语句表示把表notific中的source字段的类型修改为varchar(50),不会影响到已插入的数据
倒序查找
select * from latest order by id desc
desc 表示降序,asc 表示升序
Note
select * from latest
可能会默认升序排列也有可能是其他顺序
limit 限定返回指定的记录数
select * from latest order by id desc limit 10
上面的SQL语句表示,从latest表中查找记录,只返回前10条