星空网 > 软件开发 > 数据库

Mysql有用的面试题

A.一道SQL语句面试题,关于group by
表内容:
2005-05-09 胜
2005-05-09 胜
2005-05-09 负
2005-05-09 负
2005-05-10 胜
2005-05-10 负
2005-05-10 负

如果要生成下列结果, 该如何写sql语句?

胜 负
2005-05-09 2 2
2005-05-10 1 2
******************************************************
create table #tmp(rq varchar(10),shengfu nchar(1))

insert into #tmp values('2005-05-09','胜')
insert into #tmp values('2005-05-09','胜')
insert into #tmp values('2005-05-09','负')
insert into #tmp values('2005-05-09','负')
insert into #tmp values('2005-05-10','胜')
insert into #tmp values('2005-05-10','负')
insert into #tmp values('2005-05-10','负')
==============================================================
SELECT rq,sum(shengfu='胜') as '胜',sum(shengfu='负') as '负'
FROM `#tmp`
GROUP BY `#tmp`.rq
*****************************************************************

B.请教一个面试中遇到的SQL语句的查询问题
表中有A B C三列,用SQL语句实现:当A列大于B列时选择A列否则选择B列,当B列大于C列时选择B列否则选择C列。
------------------------------------------
select (case when a>b then a else b end),
(case when b>c then b esle c end)
from taname
***************************************************

C.面试题:一个日期判断的sql语句?
请取出tb_send表中日期(SendTime字段)为当天的所有记录?(SendTime字段为datetime型,包含日期与时间)
-----------------------------------------------------------------------------------------------------------------------
select * from time where datediff(SendTime,CURDATE())=0

CURDATE()获取当前日期
DATEDIFF() 返回起始时间 expr和结束时间expr2之间的天数。Expr和expr2 为日期或 date-and-time 表达式。计算中只用到这些值的日期部分。

TIMEDIFF(expr,expr2)
TIMEDIFF() 返回起始时间 expr 和结束时间expr2 之间的时间。 expr 和expr2 为时间或 date-and-time 表达式,两个的类型必须一样。
**************************************************************************************************************************

D.有一张表,里面有3个字段:语文,数学,英语。其中有3条记录分别表示语文70分,数学80分,英语58分,请用一条sql语句查询出这三条记录并按以下条件显示出来(并写出您的思路):
大于或等于80表示优秀,大于或等于60表示及格,小于60分表示不及格。
显示格式:
语文 数学 英语
及格 优秀 不及格
----------------------------------------------------------------------
1、一定有分三类,语文、数学、英语
2、对查询到的分数进行判断
3、先查出一个字段显示
SELECT
CASE WHEN
yuwen >= '80' THEN'优秀'WHEN yuwen >='60' THEN'及格'ELSE'不及格'END yuwen,
CASE WHEN
shuxue >= '80' THEN'优秀'WHEN shuxue >='60' THEN'及格'ELSE'不及格'END shuxue,
CASE WHEN
yingyu >= '80' THEN'优秀'WHEN yingyu >='60' THEN'及格'ELSE'不及格'END yingyu
FROM
kecheng

E.请用一个sql语句得出结果
从table1,table2中取出如table3所列格式数据,注意提供的数据及结果不准确,只是作为一个格式向大家请教。
如使用存储过程也可以。

table1

月份mon 部门dep 业绩yj
-------------------------------
一月份 01 10
一月份 02 10
一月份 03 5
二月份 02 8
二月份 04 9
三月份 03 8

table2

部门dep 部门名称dname
--------------------------------
01 国内业务一部
02 国内业务二部
03 国内业务三部
04 国际业务部

table3 (result)

部门dep 一月份 二月份 三月份
--------------------------------------
01         10       null    null
02         10       8       null
03         null      5          8
04         null     null       9
------------------------------------------

select a.dname, a.dep,
sum(case when b.mon=1 then b.yj else 0 end) as '一月份',
sum(case when b.mon=2 then b.yj else 0 end) as '二月份',
sum(case when b.mon=3 then b.yj else 0 end) as '三月份'
from table2 a left join table1 b on a.dep=b.dep
GROUP BY
a.dep

 

 

F.一道面试题
一个表中的Id有多个记录,把所有这个id的记录查出来,并显示共有多少条记录数。
------------------------------------------
select id, Count(*) from tb group by id having count(*)>1
select*from(select count(ID) as count from table group by ID)T where T.count>1

G、table表形式如下:
Year Salary
2000 1000
2001 2000
2002 3000
2003 4000
想得到如下形式的查询结果
Year Salary
2000 1000
2001 3000
2002 6000
2003 10000
sql语句怎么写?
****************************************
SELECT a.year, SUM(b.salary) AS sala
FROM table AS a,table AS b
WHERE b.salary<=a.salary
GROUP BY a.salary
***************************************************
H.用一条SQL语句查询出每门课都大于80分的学生姓名
name kecheng fenshu
张三 语文 81
张三 数学 75
李四 语文 76
李四 数学 90
王五 语文 81
王五 数学 100
王五 英语 90

****************************************************************************
selet name from tablename where name in (select name from tablename where fenshu >80 )
select distinct name from table where name not in (select distinct name from table where fenshu<=80)

 

**************************************************
I.学生表 如下:
自动编号 学号 姓名 课程编号 课程名称 分数
1 2005001 张三 0001 数学 69
2 2005002 李四 0001 数学 89
3 2005001 张三 0001 数学 69
删除除了自动编号不同,其他都相同的学生冗余信息
*******************************************************
delete tablename where 自动编号 not in() 能运行
select min(自动编号) from tablename group by 学号,姓名,课程编号,课程名称,分数 能运行
delete tablename where 自动编号 not in(select min(自动编号) from tablename group by 学号,姓名,课程编号,课程名称,分数 )

运行不了。会报《 You can't specify target table 'tb' for update in FROM clause》这样的错误
----------------------------------------------------------------------------------------------------------------------------
J.一个叫department的表,里面只有一个字段name,一共有4条纪录,分别是a,b,c,d,对应四个球对,现在四个球对进行比赛,用一条sql语句显示所有可能的比赛组合.
你先按你自己的想法做一下,看结果有我的这个简单吗?
--------------------------------------------------
答:select a.name, b.name
from team a, team b
where a.name < b.name


********************************************************************************************************************************
K.请用SQL语句实现:从TestDB数据表中查询出所有月份的发生额都比101科目相应月份的发生额高的科目。请注意:TestDB中有很多科目,都有1-12月份的发生额。
AccID:科目代码,Occmonth:发生额月份,DebitOccur:发生额。
数据库名:JcyAudit,数据集:Select * from TestDB

答:select a.*
from TestDB a
,(select Occmonth,max(DebitOccur) Debit101ccur from TestDB where AccID='101' group by Occmonth) b
where a.Occmonth=b.Occmonth and a.DebitOccur>b.Debit101ccur

 


************************************************************************************
L.面试题:怎么把这样一个表儿
year month amount
1991 1 1.1
1991 2 1.2
1991 3 1.3
1991 4 1.4
1992 1 2.1
1992 2 2.2
1992 3 2.3
1992 4 2.4
查成这样一个结果
year m1 m2 m3 m4
1991 1.1 1.2 1.3 1.4
1992 2.1 2.2 2.3 2.4

******************************************************
答案一、
select year,
(select amount from aaa m where month=1 and m.year=aaa.year) as m1,
(select amount from aaa m where month=2 and m.year=aaa.year) as m2,
(select amount from aaa m where month=3 and m.year=aaa.year) as m3,
(select amount from aaa m where month=4 and m.year=aaa.year) as m4
from aaa group by year

这个是ORACLE 中做的:
select * from (select name, year b1, lead(year) over
(partition by name order by year) b2, lead(m,2) over(partition by name order by year) b3,rank()over(
partition by name order by year) rk from t) where rk=1;




原标题:Mysql有用的面试题

关键词:MYSQL

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流