insert into articles values
(null,'第一条','张三','1998-10-10 12:32:32',null),
(null,'第二条','张三','1998-10-10 12:34:32',null),
答案:
select a.title,a.postuser,
(select max(postdate) from articles where parentid=a.id)reply
from articles a where a.parentid is null;
注释:子查询可以用在选择列中,也可用于where的比较条件中,还可以用于from从句中。
3.删除除了id号不同,其他都相同的学生冗余信息
2.学生表 如下:
id号 学号 姓名 课程编号 课程名称 分数
1 2005001 张三 0001 数学 69
2 2005002 李四 0001 数学 89
3 2005001 张三 0001 数学 69
A: delete from tablename where id号 not in(select min(id号) fromtablename group by 学号,姓名,课程编号,课程名称,分数)
实验:
create table student2(id int auto_increment primary key,codevarchar(20),name varchar(20));
insert into student2values(null,'2005001','张三'),(null,'2005002','李四'),(null,'2005001','张三');
//如下语句,mysql报告错误,可能删除依赖后面统计语句,而删除又导致统计语句结果不一致。
delete from student2 where id not in(select min(id) from student2group by name);
//但是,如下语句没有问题:
select * from student2 where id not in(select min(id) from student2group by name);
//于是,我想先把分组的结果做成虚表,然后从虚表中选出结果,最后再将结果作为删除的条件数据。
delete from student2 where id not in(select mid from (selectmin(id) mid
from student2 group by name) as t);
或者:
delete from student2 where id not in(select min(id) from (select *from s
tudent2) as t group by t.name);
4.航空网的几个航班查询题:
表结构如下:
flight{flightID,StartCityID ,endCityID,StartTime}
city{cityID, CityName)
实验环境:
create table city(cityID int auto_increment primary key,cityNamevarchar(20));
create table flight (flightID int auto_increment primary key,
StartCityID int references city(cityID),
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-34607-9.html
还好俺是穷光蛋
不腻么
#舞蹈担当易烊千玺#