MySQl数据库必知必会sql语句_Mysql_脚本之家

这一篇属于加强版,问题和sql语句如下。

创建users表,设置id,name,gender,sal字段,其中id为主键

drop table if exists users; create table if not exists users primary key auto_increment, name varchar unique not null, gender varchar not null, sal int; insert into users values; insert into users values; 

一对一:AA的身份号是多少

drop table if exists users; create table if not exists users primary key auto_increment, name varchar unique not null, gender varchar not null, sal int; insert into users values; insert into users values; drop table if exists cards; create table if not exists cards primary key auto_increment, num int not null unique, loc varchar not null, uid int not null unique, constraint uid_fk foreign key references users; insert into cards values; insert into cards values; 

select u.name "姓名",c.num "身份证号" from users u inner join cards c on u.id = c.uid where u.name = 'AA'; -- select u.name "姓名",c.num "身份证号" from users u inner join cards c on u.id = c.uid where name = 'AA'; 

一对多:查询”开发部”有哪些员工

drop table if exists groups; create table if not exists groups primary key auto_increment, name varchar; insert into groups; insert into groups; 

drop table if exists emps; create table if not exists emps primary key auto_increment, name varchar not null, gid int not null, constraint gid_fk foreign key references groups; insert into emps values; insert into emps values; insert into emps values; insert into emps values; 

查询”开发部”有哪些员工

select g.name "部门",e.name "员工" from groups g inner join emps e on g.id = e.gid where g.name = '开发部'; -- select g.name "部门",e.name "员工" from groups g inner join emps e on g.id = e.gid where g.name = '开发部'; 

多对多:查询”赵”教过哪些学生

创建students表

drop table if exists students; create table if not exists students primary key auto_increment, name varchar; insert into students; insert into students; 

创建teachers表

drop table if exists teachers; create table if not exists teachers primary key auto_increment, name varchar; insert into teachers; insert into teachers; 

创建middles表 primary key 表示联合主键,这两个字段的整体要唯一

drop table if exists middles; create table if not exists middles, constraint sid_fk foreign key references students, constraint tid_fk foreign key references teachers, primary key; insert into middles; insert into middles; insert into middles; insert into middles; 

查询”赵”教过哪些学生

select t.name "老师",s.name "学生" from students s inner join middles m inner join teachers t on  and  where t.name = '赵'; -- select t.name "老师",s.name "学生" from students s inner join middles m inner join teachers t on  and  where t.name = "赵"; 

将5000元以上的员工标识为”高薪”,否则标识为”起薪”

将薪水为NULL的员工标识为”无薪”

将5000元以上的员工标识为”高薪”,否则标识为”起薪”

将7000元的员工标识为”高薪”,6000元的员工标识为”中薪”,5000元则标识为”起薪”,否则标识为”试用薪”


内连接:查询客户姓名,订单编号,订单价格

【注:customers c inner join orders o使用了别名,以后o就代表orders】

select c.name "客户姓名",o.isbn "订单编号",o.price "订单价格" from customers c inner join orders o on c.id = o.customers_id; -- select c.name "客户姓名",o.isbn "订单编号",o.price "订单价格" from customers c inner join orsers o on c.id = o.customers_id; 

on+两张表连接的条件.一张表的主键,一张表的外键

内连接:只能查询出二张表中根据连接条件都存在的记录,有点类似于数学中交集


外连接:按客户分组,查询每个客户的姓名和订单数

外连接:既可以根据连接条件查询出二张表中都存在的记录,也能根据一方,强行将另一方就算不满兄条件的记录也能查询出来

右外连接 : 以右侧为参照,right outer join表示 select c.name,count from orders o right outer join customers c on c.id = o.customers_id group by c.name; 

left outer join表示左边的内容都会显现出来,例如customers c left out join
表示会把customers中的某列所有内容都找出来


自连接:求出AA的老板是EE。把自己想象成两张表。左右各一张

select users.ename,bosss.ename from emps users inner join emps bosss on users.mgr = bosss.empno; select users.ename,bosss.ename from emps users left outer join emps bosss on users.mgr = bosss.empno; 

演示MySQL中的函数

select addtime('2016-8-7 23:23:23','1:1:1'); 时间相加 select current_date(); select current_time; select year; select month; select day; select datediff; 

select charset; select concat; select instr('www.baidu.com','baidu'); select substring; 

select bin; select floor;//比3.14小的最大整数---正3 select floor;//比-3.14小的最大整数---负4 select ceiling;//比3.14大的最小整数---正4 select ceiling;//比-3.14大的最小整数---负3,一定是整数值 select format;保留小数点后3位,四舍五入 select mod;//取余数 select rand();// 

select md5;

返回32位16进制数 e10adc3949ba59abbe56e057f20f883e

演示MySQL中流程控制语句

use json; drop table if exists users; create table if not exists users primary key auto_increment, name varchar not null unique, sal int; insert into users values; insert into users values; insert into users values; insert into users values; insert into users values; insert into users values; insert into users values; insert into users values; insert into users values; 

将5000元以上的员工标识为”高薪”,否则标识为”起薪”

select name "姓名",sal "薪水", if "描述" from users; 

将薪水为NULL的员工标识为”无薪”

select name "姓名",ifnull "薪水" from users; 

将5000元以上的员工标识为”高薪”,否则标识为”起薪”

select name "姓名",sal "薪水", case when sal>=5000 then "高薪" else "起薪" end "描述" from users; 

将7000元的员工标识为”高薪”,6000元的员工标识为”中薪”,5000元则标识为”起薪”,否则标识为”试用薪”

select name "姓名",sal "薪水", case sal when 3000 then "低薪" when 4000 then "起薪" when 5000 then "试用薪" when 6000 then "中薪" when 7000 then "较好薪" when 8000 then "不错薪" when 9000 then "高薪" else "重薪" end "描述" from users;

以上所述是小编给大家介绍的MySQl数据库必知必会sql语句,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

发表评论

电子邮件地址不会被公开。 必填项已用*标注