【1.having和where的区别】
都是过滤,但是where后边跟的表达式,having和聚合函数group by结合使用(比如从很多数据中看性别男几人,女几人)使用。
【2.请写出删除和插入的SQL语句】
1.首先创建一个表格,插入部分4种方式,删除部分3种方式。
2.首先插入包括:
-
按字段插入:
insert/replace into 表名称(列表名称) values(”zxx“);
-
全部插入:
insret/replace into 表名称 values ("zxx",18);
-
批量插入:
insert/replace into 表名称 values("A",18),("B",20),("C",25);
replace into user values(1,"zx",20,"chengdu"),(2,"ly",25,"xian");
查看是否插入成功:
select * from tableName; 查询插入是否成功
-
多用replace插入,覆盖重复数据
3.然后删除部分包括:
-
针对大量表格内容全部删除:(更快)
truncate table 表名;
truncate table user;
-
针对表格内容全部删除:
delete from 表名;
delete from user;
-
针对某一条的删除:
delete from 表名 where id=X;
delete from user where id=1;
【3.mysql中有两个表,表A中有name,性别,分数;表B中有name,性别,分数。
1.把A、B表中的女生人数查询出来,
查询表中男女总数各有几人select sex,count(sex) from person group by sex;
过滤出性别男女的总数select gender,count(gender) from employees group by gender;
2.再用聚合函数相加使用select(1+2);
【4.平均分,工程用到哪些函数】
A、B表的平均分都查出来,除以2使用命令 select (1+2)/2;
查平均分:select score,avg(score) as 平均分数 from person group by sex;
【5.MySQL中有两个表,表A中有id,name,company,表B中有id,name和薪资,怎么查询李四的工资】
通过表关联内连接的方式查询。通过共同字段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";
【6.创建表约束if not exists的意义】
create table if not exists A (name varchar(100));如果表A已经存在,执行此命令不会出错、不会创建新的A表覆盖原来的表。
【7.一个表中怎么设置多个主键】
比如创建表格asd,把名字和id都设置为主键
create table asd id (int,name varchar(20) primary key(id,name));
查询创建过程
show create table asd \G;
【8.怎么通过学生成绩判断等级】
通过case when的方式来设置条件,then判断结果,全部输入完end结束,再筛选查找
命令:select (case when score=100 then "满分" when score>=85 and score<100 then "优秀" when score>=60 and score<85 then "良好" when score<60 then "不及格" end)socre,name from student;
【9.修改了端口怎么登陆】
mysql -h localhost(到时候给的地址,自己就用这个本地) -u root(给的账户) -p
【10.分开性别怎么各自统计人数】
select sex, count(1) as 总人数 from person group by sex;
【11.查询出重复姓名大于1的人的信息】
select name from person group by name having count(*/1)>1
子查询方式:
select * from person where name in(select name from person group by name having count(*/1)>1);
去重
select distinct name from person where name in(select name from person group by name having count(*)>1);
【12.查询出不同性别的年龄总和】
select sex,sum(age) as 年龄总和 from person group by sex;
【13.查询不同性别的平均年龄】
select sex,avg(age) as 平均年龄 from person group by sex;