1.表内容如下
要生成下列结果,该如何写SQL语句?(自定义表,将对应的数据导入到自定义表中,然后查询出对应的数据)
该题主要是考临时表的使用,数据的导入及查询语句与group by的使用。
sql
Drop table if exists `competition`;
Create temporary table `competition`(
`id` int(20) not null auto_increment,
`name` varchar(10) default null,
`value` varchar(10) default null,
Primary key (`id`)
);
insert into competition(name, value) values('A','胜'),('A','胜'),('A','负'),('A','负'),('B','胜'),('B','胜'),('B','胜'),('C','负');
select c.name, sum(case when c.value='胜' then 1 else 0 end) as '胜', sum(case when c.value='负' then 1 else 0 end) as '负'
from competition c
group by c.name;
2.表Template中有A、B、C三列,用SQL语句实现,当列A大于列B时选择A列否则选择B列,当列B大于列C时选择B列否则选择C列。 这道题主要是考 case when的使用。
sql
select
Case when t.A > t.B then t.A
Else t.B
End
Case when t.B > t.C then t.B
Else t.C
End
from Template t;
1
2
3
4
5
6
7
8
9
10
3.将表temp1中t1字段的值更新为temp2中t2的值,他们的关联字段为id; 这道题主要是考update的使用。
sql
update temp1, temp2 set temp1 .t1 = temp2.t2 where temp1.id = temp2.id;
1
2
4.将人员表中A01中参加工作时间A0141小于等于“1987-10-30”的数据删除;这道题主要是考delete语句的使用。
sql
delete from A01 where A0141 <= ‘1987-10-30’;
5.字符串“abcd123”,分别取出该字符串前四个字符,右边三个字符。这道题主要考取部分字符串的函数使用。
sql
#取前四个字符
select left('abcd123', 4) as a;
#取右边四个字符
select right('abcd123', 3) as b;
---------------------
作者:RED-GO
来源:CSDN
原文:https://blog.csdn.net/u010137431/article/details/50005193
版权声明:本文为博主原创文章,转载请附上博文链接!