本文給大家分享了一篇關於mysql資料庫必會sql語句加強版內容,非常不錯,具有參考借鑒價值,需要的朋友參考下吧
這一篇屬於加強版,問題和sql語句如下。
建立users表,設定id,name,gender,sal字段,其中id為主鍵
drop table if exists users; create table if not exists users( id int(5) primary key auto_increment, name varchar(10) unique not null, gender varchar(1) not null, sal int(5) not null ); insert into users(name,gender,sal) values('AA','男',1000); insert into users(name,gender,sal) values('BB','女',1200);
------- -------------------------------------------------- -----------------------------
一對一:AA的識別號碼是多少
drop table if exists users; create table if not exists users( id int(5) primary key auto_increment, name varchar(10) unique not null, gender varchar(1) not null, sal int(5) not null ); insert into users(name,gender,sal) values('AA','男',1000); insert into users(name,gender,sal) values('BB','女',1200); drop table if exists cards; create table if not exists cards( id int(5) primary key auto_increment, num int(3) not null unique, loc varchar(10) not null, uid int(5) not null unique, constraint uid_fk foreign key(uid) references users(id) ); insert into cards(num,loc,uid) values(111,'北京',1); insert into cards(num,loc,uid) values(222,'上海',2);
【註:inner join表示內連接】
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( id int(5) primary key auto_increment, name varchar(10) not null ); insert into groups(name) values('开发部'); insert into groups(name) values('销售部');
#
drop table if exists emps; create table if not exists emps( id int(5) primary key auto_increment, name varchar(10) not null, gid int(5) not null, constraint gid_fk foreign key(gid) references groups(id) ); insert into emps(name,gid) values('哈哈',1); insert into emps(name,gid) values('呵呵',1); insert into emps(name,gid) values('嘻嘻',2); insert into emps(name,gid) values('笨笨',2);
#
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( id int(5) primary key auto_increment, name varchar(10) not null ); insert into students(name) values('哈哈'); insert into students(name) values('嘻嘻');
#
drop table if exists teachers; create table if not exists teachers( id int(5) primary key auto_increment, name varchar(10) not null ); insert into teachers(name) values('赵'); insert into teachers(name) values('刘');
#
drop table if exists middles; create table if not exists middles( sid int(5), constraint sid_fk foreign key(sid) references students(id), tid int(5), constraint tid_fk foreign key(tid) references teachers(id), primary key(sid,tid) ); insert into middles(sid,tid) values(1,1); insert into middles(sid,tid) values(1,2); insert into middles(sid,tid) values(2,1); insert into middles(sid,tid) values(2,2);
#
select t.name "老师",s.name "学生" from students s inner join middles m inner join teachers t on (s.id=m.sid) and (m.tid=t.id) where t.name = '赵'; -- select t.name "老师",s.name "学生" from students s inner join middles m inner join teachers t on (s.id=m.sid) and (t.id=m.tid) where t.name = "赵";
將薪水為NULL的員工標識為"無薪"
#將5000元(含)以上的員工標識為"高薪",否則標識為"起薪"
將7000元的員工標識為"高薪",6000元的員工識別為"中薪",5000元則標識為"起薪",否則標識為"試用薪"
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;
<左外连接 : 以左侧为参照,left outer join表示 select c.name,count(o.isbn) from customers c left outer join orders o on c.id = o.customers_id group by c.name; -- >右外连接 : 以右侧为参照,right outer join表示 select c.name,count(o.isbn) from orders o right outer join customers c on c.id = o.customers_id group by c.name;
自連接:求出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 now(); select year( now() ); select month( now() ); select day( now() ); select datediff('2016-12-31',now());
#
select charset('哈哈'); select concat('你好','哈哈','吗'); select instr('www.baidu.com','baidu'); select substring('www.baidu.com',5,3);
select bin(10); select floor(3.14);//比3.14小的最大整数---正3 select floor(-3.14);//比-3.14小的最大整数---负4 select ceiling(3.14);//比3.14大的最小整数---正4 select ceiling(-3.14);//比-3.14大的最小整数---负3,一定是整数值 select format(3.1415926,3);保留小数点后3位,四舍五入 select mod(10,3);//取余数 select rand();//
use json; drop table if exists users; create table if not exists users( id int(5) primary key auto_increment, name varchar(10) not null unique, sal int(5) ); insert into users(name,sal) values('哈哈',3000); insert into users(name,sal) values('呵呵',4000); insert into users(name,sal) values('嘻嘻',5000); insert into users(name,sal) values('笨笨',6000); insert into users(name,sal) values('明明',7000); insert into users(name,sal) values('丝丝',8000); insert into users(name,sal) values('君君',9000); insert into users(name,sal) values('赵赵',10000); insert into users(name,sal) values('无名',NULL);
#為"高薪",否則標識為"起薪"
select name "姓名",sal "薪水", if(sal>=5000,"高薪","起薪") "描述" from users;
#將薪水為NULL的員工標識為"無薪"
select name "姓名",ifnull(sal,"无薪") "薪水" from users;
#將5000元(含)以上的員工標識為"高薪",否則標識為"起薪"
select name "姓名",sal "薪水", case when sal>=5000 then "高薪" else "起薪" end "描述" from users;
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語句的詳細內容。更多資訊請關注PHP中文網其他相關文章!