主頁(yè) > 知識(shí)庫(kù) > MySQl數(shù)據(jù)庫(kù)必知必會(huì)sql語(yǔ)句(加強(qiáng)版)

MySQl數(shù)據(jù)庫(kù)必知必會(huì)sql語(yǔ)句(加強(qiáng)版)

熱門(mén)標(biāo)簽:智能電銷(xiāo)機(jī)器人靠譜么 地圖標(biāo)注培訓(xùn) 南宋地圖標(biāo)注黃河華山 手機(jī)用地圖標(biāo)注工具 電銷(xiāo)機(jī)器人說(shuō)明書(shū) 昆明智能外呼系統(tǒng)中心 電銷(xiāo)機(jī)器人公眾號(hào)推送 安國(guó)在哪里辦理400電話 長(zhǎng)安區(qū)違法建房地圖標(biāo)注

這一篇屬于加強(qiáng)版,問(wèn)題和sql語(yǔ)句如下。

創(chuàng)建users表,設(shè)置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); 

-------------------------------------------------------------------------------------- 

一對(duì)一:AA的身份號(hào)是多少 

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表示內(nèi)連接】 

select u.name "姓名",c.num "身份證號(hào)" 
from users u inner join cards c 
on u.id = c.uid 
where u.name = 'AA'; 
-- 
select u.name "姓名",c.num "身份證號(hào)" 
from users u inner join cards c 
on u.id = c.uid 
where name = 'AA'; 

--------------------------------------------- 

一對(duì)多:查詢(xún)"開(kāi)發(fā)部"有哪些員工 

創(chuàng)建groups表 

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('開(kāi)發(fā)部'); 
insert into groups(name) values('銷(xiāo)售部'); 

創(chuàng)建emps表 

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); 

查詢(xún)"開(kāi)發(fā)部"有哪些員工 

select g.name "部門(mén)",e.name "員工" 
from groups g inner join emps e 
on g.id = e.gid 
where g.name = '開(kāi)發(fā)部'; 
-- 
select g.name "部門(mén)",e.name "員工" 
from groups g inner join emps e 
on g.id = e.gid 
where g.name = '開(kāi)發(fā)部'; 

------------------------------------------------------ 

多對(duì)多:查詢(xún)"趙"教過(guò)哪些學(xué)生 

創(chuàng)建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('嘻嘻'); 

創(chuàng)建teachers表 

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('劉'); 

創(chuàng)建middles表   primary key(sid,tid)  表示聯(lián)合主鍵,這兩個(gè)字段的整體要唯一 

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); 

查詢(xún)"趙"教過(guò)哪些學(xué)生 

select t.name "老師",s.name "學(xué)生" 
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 "學(xué)生" 
from students s inner join middles m inner join teachers t  
on (s.id=m.sid) and (t.id=m.tid) 
where t.name = "趙"; 

-------------------------------------------------------------------------------------------------------- 

將5000元(含)以上的員工標(biāo)識(shí)為"高薪",否則標(biāo)識(shí)為"起薪" 

將薪水為NULL的員工標(biāo)識(shí)為"無(wú)薪" 

將5000元(含)以上的員工標(biāo)識(shí)為"高薪",否則標(biāo)識(shí)為"起薪" 

將7000元的員工標(biāo)識(shí)為"高薪",6000元的員工標(biāo)識(shí)為"中薪",5000元?jiǎng)t標(biāo)識(shí)為"起薪",否則標(biāo)識(shí)為"試用薪"

--------------------------------------------------------------------------------------------------------- 

內(nèi)連接(等值連接):查詢(xún)客戶(hù)姓名,訂單編號(hào),訂單價(jià)格 

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

select c.name "客戶(hù)姓名",o.isbn "訂單編號(hào)",o.price "訂單價(jià)格" 
from customers c inner join orders o 
on c.id = o.customers_id; 
-- 
select c.name "客戶(hù)姓名",o.isbn "訂單編號(hào)",o.price "訂單價(jià)格" 
from customers c inner join orsers o 
on c.id = o.customers_id; 

on+兩張表連接的條件.一張表的主鍵,一張表的外鍵 

內(nèi)連接:只能查詢(xún)出二張表中根據(jù)連接條件都存在的記錄,有點(diǎn)類(lèi)似于數(shù)學(xué)中交集 

---------------------------------------------------- 

外連接:按客戶(hù)分組,查詢(xún)每個(gè)客戶(hù)的姓名和訂單數(shù) 

外連接:既可以根據(jù)連接條件查詢(xún)出二張表中都存在的記錄,也能根據(jù)一方,強(qiáng)行將另一方就算不滿(mǎn)兄條件的記錄也能查詢(xún)出來(lái) 

外連接可以細(xì)分為:

左外連接 : 以左側(cè)為參照,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; 
-- 
>右外連接 : 以右側(cè)為參照,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; 

left outer join表示左邊的內(nèi)容都會(huì)顯現(xiàn)出來(lái),例如customers c left out join 表示會(huì)把customers中的某列所有內(nèi)容都找出來(lái)  

------------------------------------------------------ 
自連接:求出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中的函數(shù)(查詢(xún)手冊(cè))  

日期時(shí)間函數(shù): 

select addtime('2016-8-7 23:23:23','1:1:1');  時(shí)間相加 
select current_date(); 
select current_time(); 
select now(); 
select year( now() ); 
select month( now() ); 
select day( now() ); 
select datediff('2016-12-31',now()); 

字符串函數(shù):

select charset('哈哈'); 
select concat('你好','哈哈','嗎'); 
select instr('www.baidu.com','baidu'); 
select substring('www.baidu.com',5,3); 

數(shù)學(xué)函數(shù): 

select bin(10); 
select floor(3.14);//比3.14小的最大整數(shù)---正3 
select floor(-3.14);//比-3.14小的最大整數(shù)---負(fù)4 
select ceiling(3.14);//比3.14大的最小整數(shù)---正4 
select ceiling(-3.14);//比-3.14大的最小整數(shù)---負(fù)3,一定是整數(shù)值 
select format(3.1415926,3);保留小數(shù)點(diǎn)后3位,四舍五入 
select mod(10,3);//取余數(shù) 
select rand();// 

加密函數(shù): 

select md5('123456'); 

返回32位16進(jìn)制數(shù) e10adc3949ba59abbe56e057f20f883e  

演示MySQL中流程控制語(yǔ)句 

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('無(wú)名',NULL); 

將5000元(含)以上的員工標(biāo)識(shí)為"高薪",否則標(biāo)識(shí)為"起薪"

select name "姓名",sal "薪水", 
    if(sal>=5000,"高薪","起薪") "描述" 
from users; 

將薪水為NULL的員工標(biāo)識(shí)為"無(wú)薪"

select name "姓名",ifnull(sal,"無(wú)薪") "薪水" 
from users; 

將5000元(含)以上的員工標(biāo)識(shí)為"高薪",否則標(biāo)識(shí)為"起薪"

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

將7000元的員工標(biāo)識(shí)為"高薪",6000元的員工標(biāo)識(shí)為"中薪",5000元?jiǎng)t標(biāo)識(shí)為"起薪",否則標(biāo)識(shí)為"試用薪"

select name "姓名",sal "薪水", 
    case sal 
      when 3000 then "低薪" 
      when 4000 then "起薪" 
      when 5000 then "試用薪" 
      when 6000 then "中薪" 
      when 7000 then "較好薪" 
      when 8000 then "不錯(cuò)薪" 
      when 9000 then "高薪" 
      else "重薪" 
    end "描述" 
from users;

以上所述是小編給大家介紹的MySQl數(shù)據(jù)庫(kù)必知必會(huì)sql語(yǔ)句(加強(qiáng)版),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

您可能感興趣的文章:
  • 在MySQL數(shù)據(jù)庫(kù)中使用C執(zhí)行SQL語(yǔ)句的方法
  • 六條比較有用的MySQL數(shù)據(jù)庫(kù)操作的SQL語(yǔ)句小結(jié)
  • MySql數(shù)據(jù)庫(kù)之a(chǎn)lter表的SQL語(yǔ)句集合
  • Mysql數(shù)據(jù)庫(kù)之sql基本語(yǔ)句小結(jié)
  • MySQL5.7.14下載安裝圖文教程及MySQL數(shù)據(jù)庫(kù)語(yǔ)句入門(mén)大全
  • 詳解MySQL數(shù)據(jù)庫(kù)insert和update語(yǔ)句
  • 優(yōu)化MySQL數(shù)據(jù)庫(kù)中的查詢(xún)語(yǔ)句詳解
  • mysql數(shù)據(jù)庫(kù)重命名語(yǔ)句分享
  • MySQL數(shù)據(jù)庫(kù)INSERT、UPDATE、DELETE以及REPLACE語(yǔ)句的用法詳解
  • Mysql數(shù)據(jù)庫(kù)之常用sql語(yǔ)句進(jìn)階與總結(jié)

標(biāo)簽:吉安 南昌 合肥 潛江 東莞 長(zhǎng)沙 武漢 江門(mén)

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《MySQl數(shù)據(jù)庫(kù)必知必會(huì)sql語(yǔ)句(加強(qiáng)版)》,本文關(guān)鍵詞  MySQl,數(shù)據(jù)庫(kù),必知,必會(huì),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《MySQl數(shù)據(jù)庫(kù)必知必會(huì)sql語(yǔ)句(加強(qiáng)版)》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于MySQl數(shù)據(jù)庫(kù)必知必會(huì)sql語(yǔ)句(加強(qiáng)版)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章