MySQL面试题

 

修改了端口怎么登陆

mysql -h localhost(修改为到时候给的地址) -u root(给的账户) -p

 

●请写出删除和插入的Sql语句

①首先创建一个表格,插入部分4种方式,删除部分3种方式。

②插入包括:

- 按字段插入:

insert/replace into 表名称(列表名称) valuesxxx);

- 全部插入:

insret/replace into 表名称 values ("xxx",18);

- 批量插入:

insert/replace into 表名称 values("A",18),("B",20),("C",25);

replace into user values(1,"xx",20,"chengdu"),(2,"xy",25,"xian");

查看是否插入成功:

select * from tableName; 查询插入是否成功

- 多用replace插入,覆盖重复数据

③然后删除部分包括:

- 针对大量表格内容全部删除:(更快)

truncate table 表名;

truncate table user;

- 针对表格内容全部删除:

delete from 表名;

delete from user;

- 针对某一条的删除:

delete from 表名 where id=X;

delete from user where id=1;

 

havingwhere的区别

where后边跟的是表达式

having聚合函数结合起来使用 group by

 

平均分,工程用到哪些函数

AB表的平均分都查出来,除以2使用命令 select 1+2/2;

 

mysql中有两个表,表A中有name,性别,分数;表B中有name,性别,分数

①把AB表中的女生人数查询出来,

查询表中男女总数各有几人select sex,count(sex) from person group by sex;

过滤出性别男女的总数select gender,count(gender) from employees group by gender;

②再用聚合函数相加使用select(1+2);

 

MySQL中有两个表,表A中有idnamecompany,表B中有idname和薪资,怎么查询李四的工资

通过表关联内连接的方式查询。通过共同字段id把两个表关联起来,where后边跟的是“lisi,前边的select部分是B表的salary薪资部分。具体表达式:

select b.salary from A a inner join B b on a.id=b.id where a.name="lisi";

 

●创建表约束if not exists的意义

create table if not exists a (name varchar(10));

创建表的时候不会更改已有的表,并且创建表的时候不会出错,如过创建的表已经存在,则不会创建新的表

 

●一个表怎么设置为主键

比如创建表格asd,把名字和id都设置为主键

 create table asd

    -> (

    -> id int,

    -> name varchar(10),

    -> primary key(id,name)

    -> );

 

 

●分开性别怎么各自统计人数

select sex,count(1) as 人数 from person group by sex;

 

怎么通过学生成绩判断等级

首先创建一个含有学生信息的数据

 

insert into student values
-> (1,"xw",90),
-> (2,"wx",85),
-> (3,"zx",25),
-> (4,"xz",100),
-> (5,"xc",77),
-> (6,"cx",66),
-> (7,"cs",55);

然后通过case when的方式来设置条件,then判断结果,全部输入完end结束,再筛选查找

 select

    -> (

    -> case when score=100 then "满分"

    -> when score>=80 and score<=90 then "优秀"

    -> when score>=60 and score<80 then "良好"

    -> when score<60 then "小傻子" end

    -> )

-> score,name from student;

 

 

●查询名字相同的人

①select name from person group by name having count(*)>1 →(只能查询到名字)

②select * from person

    -> where name in

    -> (select name from person group by name having count(*)>1

-> );→(子查询可以查看到名字相同的人的所有信息)

③select distinct name,sex,age from person where name in (select name from person group by name having count(*)>1);→(也可以用distinct控制想查看的信息)

 

●查询不同性别年龄的总和

select sex,sum(age) sum from person group by sex;

 

●查询出不同性别的平均年龄

select sex,avg(age) avg from person group by sex;

 

posted @ 2022-08-29 17:33  丶空·  阅读(20)  评论(0)    收藏  举报