查询题 周末
1.創建留言數據庫: liuyandb;
create database liuyandb;
2.在liuyandb數據庫中創建留言表liuyan,
create table liuyan(
id int auto_increment primary key comment '編號',
title varchar(32) not null comment '標題',
author varchar(16) null comment '作者',
addtime varchar(12) not null comment '留言時間',
content text not null comment '留言內容',
isdelete tinyint not null default 0 comment '是否刪除'
)engine=innodb default charset=utf8;
注意: comment :表示注釋
3.在留言表最后添加一列狀態(status tinyint 默認值為0),???
alter table liuyan add status tinyint default 0 after isdelete;
注意:after isdelete:表示在哪個字段后添加 新字段
4.修改留言表author的默認值為’youku’,設為非空
alter table liuyan modify author varchar(16) not null default 'youku';
5.刪除liuyan表中的isdelete字段
alter table liuyan drop isdelete;
6.為留言表添加>5條測試數據
insert into liuyan values
(null,'介紹','大雄','1000','哥不是一匹好馬,但也不是一頭普通的毛驢',null),
(null,'叮當貓','熊熊','2000','你牙縫里有韭菜,扣出來賊哥吃',null),
(null,'花花','苗苗','3000','苗苗問花花:賣萌是褒義詞還是貶義詞?',null),
(null,'霞哥','雄大','4000','斗戰色佛',null),
(null,'晨晨','逗比','5000','你笑起來像一朵菊花,菊花殘,man腚傷',null);
7.要求將id值大于3的信息中author字段值改為admin
update liuyan set author='admin' where id>3;
8.刪除id號為4的數據
delete from liuyan where id=4;
附加題:
9.為留言表添加>15條測試數據,要求分三個用戶添加
懶.....
10.查詢所有留言信息
select id,title,author,addtime,content,status from liuyan;
11.查詢某一用戶的留言信息
select id,title,author,addtime,content,status from liuyan where author='用戶名稱';
12.查詢所有數據,按時間降序排序
select id,title,author,addtime,content,status from liuyan order by addtime desc;
13.獲取id在2到6之間的留言信息,并按時間降序排序
select id,title,author,addtime,content,status from liuyan where id between 2 and 6;
14.統計每個用戶留了多少條留言,并對數量按從小到大排序。
select author,count(id) as'留言條數' from liuyan group by author order by count(id) desc
15.將id為8、9的兩條數據的作者改為’doudou’.
update liuyan set author ='doudou' where id in(8,9);
16.取出最新的三條留言。(使用limit)。
select * from ( select id,title,author,addtime,content,status from liuyan ORDER BY addtime desc)hahg LIMIT 3
17.查詢留言者中包含”d”字母的留言信息,并按留言時間從小到大排序
select id,title,author,addtime,content,status from liuyan where author like'%d%' order by addtime asc;
18.刪除”作者”重復的數據,并保留id最大的一個作者
delete from liuyan where author in(
select author from (select author from liuyan group by author having count(1)>1) a
)
and id not in(
select id from (select max(id) id from liuyan group by author having count(1)>1) b
)
?
轉載于:https://www.cnblogs.com/xiaoluoboer/p/8025366.html
總結
- 上一篇: Python-多进程
- 下一篇: SUSE11 搭建iscsi targe