大学生春季运动会的数据库,保存了比赛信息的三个表以下: 运动员 sporter(运动员编号 sporterid,姓名name,性别 sex,所属系号 department), 项目 item(项目编号 itemid,名称 itemname,比赛地点 location), 成绩 grade(运动员编号 id,项目编号 itemid,积分 mark)。sql
用SQL语句完成在“体育馆”进行比赛的各项目名称及其冠军的姓名。数据库
SELECT i.itemname,s.name FROM grade g,item i,sporter s, (SELECT itemid iid,MAX(mark) max FROM grade WHERE itemid IN ( SELECT itemid FROM item WHERE location='体育馆') GROUP BY itemid) temp WHERE g.itemid=temp.iid AND g.mark=temp.max AND temp.iid=i.itemid AND s.sporterid=g.sporterid;
某打车公司将驾驶里程(drivedistanced)超过5000里的司机信息转移到一张称为seniordrivers 的表中,他们的详细状况被记录在表drivers 中,正确的sql为()数据结构
select * into seniordrivers from drivers where drivedistanced >=5000
INSERT INTO 语句用于向表格中插入新的行函数
INSERT INTO table_name VALUES (值1, 值2,....) 指定所要插入数据的列: INSERT INTO table_name (列1, 列2,...) VALUES (值1, 值2,....)
从一个表中选取数据,而后把数据插入另外一个表中操作系统
把全部的列插入新表 SELECT * INTO new_table_name [IN externaldatabase] FROM old_tablename 只把但愿的列插入新表 SELECT column_name(s) INTO new_table_name [IN externaldatabase] FROM old_tablename
有两张表,以下图所示.net
表A(仅列出部分数据做参考)code
Order_id | User_id | Add_time |
---|---|---|
11701245001 | 10000 | 1498882474 |
11701245002 | 10001 | 1498882475 |
表B:(仅列出部分数据做参考)blog
id | Order_id | goods_id | price |
---|---|---|---|
1 | 11701245001 | 1001 | 10 |
2 | 11701245001 | 1002 | 20 |
3 | 11701245002 | 1001 | 10 |
用SQL查询 购买过goods_id 为1001的用户user_id()ip
1. select a.user_id from A a,B b where a.order_id=b.order_id and b.goods_id='1001' 2. select user_id from A where order_id in (select order_id from B where goods_id = '1001') 3. select A.user_id from A left join B on A.order_id=B.order_id where B.goods_id='1001'
注意第三条语句,用的是左外链接。get
请取出 BORROW表中日期(RDATE字段)为当天的全部记录?(RDATE字段为datetime型,包含日期与时间)。
select * from BORROW where datediff(dd,RDATE,getdate())=0
DATEDIFF() 函数返回两个日期之间的时间。
语法:DATEDIFF(datepart,startdate,enddate)
startdate 和 enddate 参数是合法的日期表达式。
datepart 参数能够是下列的值:
datepart | 缩写 |
---|---|
年 | yy, yyyy |
季度 | qq, q |
月 | mm, m |
年中的日 | dy, y |
日 | dd, d |
周 | wk, ww |
星期 | dw, w |
小时 | hh |
分钟 | mi, n |
秒 | ss, s |
毫秒 | ms |
微妙 | mcs |
纳秒 | ns |
一张学生成绩表score,部份内容以下:
name | course | grade |
---|---|---|
张三 | 操做系统 | 67 |
张三 | 数据结构 | 86 |
李四 | 软件工程 | 89 |
用一条SQL 语句查询出每门课都大于80 分的学生姓名 。
1. select distinct s.name from score s where name not in (select name from score where grade <=80) 2. select distinct s.name from score s group by s.name having min(s.grade) > 80
1.where 在分组前过滤,having 在分组后过滤,二者之间不冲突。
2.在select 子句中,只能够有组函数和分组字段。若是没有分组字段那就是全部的字段均可以在select 字句中。
注意第6、七题是同款题目,都是
Date | Win |
---|---|
2017-07-12 | 胜 |
2017-07-12 | 负 |
2017-07-15 | 胜 |
2017-07-15 | 负 |
要生成下列结果 :
比赛日期 | 胜 | 负 |
---|---|---|
2017-07-12 | 1 | 1 |
2017-07-15 | 1 | 1 |
select Date as 比赛日期,sum(case when Win ='胜' 1 else 0 end) 胜,sum(case when Win='负' 1 else 0 end ) 负 from result group by Date
有一张学生成绩表sc(sno 学号,class 课程,score 成绩),请查询出每一个学生的英语、数学的成绩(行转列,一个学生只有一行记录)。
select sno , sum(if(class='english',score,0)) english ,sum(if(class='math',score,0)) math from sc where class in ('english','math') group by sno