最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

MySQl數(shù)據(jù)庫必知必會sql語句(加強版)

 更新時間:2017年04月17日 16:44:47   投稿:mrr  
本文給大家分享了一篇關(guān)于mysql數(shù)據(jù)庫必會sql語句加強版內(nèi)容,非常不錯,具有參考借鑒價值,需要的朋友參考下吧

這一篇屬于加強版,問題和sql語句如下。

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

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

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

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

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

一對多:查詢"開發(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('開發(fā)部'); 
insert into groups(name) values('銷售部'); 

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

查詢"開發(fā)部"有哪些員工 

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

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

多對多:查詢"趙"教過哪些學生 

創(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)合主鍵,這兩個字段的整體要唯一 

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 = "趙"; 

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

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

將薪水為NULL的員工標識為"無薪" 

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

將7000元的員工標識為"高薪",6000元的員工標識為"中薪",5000元則標識為"起薪",否則標識為"試用薪"

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

內(nèi)連接(等值連接):查詢客戶姓名,訂單編號,訂單價格 

【注: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+兩張表連接的條件.一張表的主鍵,一張表的外鍵 

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

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

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

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

外連接可以細分為:

<左外連接 : 以左側(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)容都會顯現(xiàn)出來,例如customers c left out join 表示會把customers中的某列所有內(nè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ù)(查詢手冊)  

日期時間函數(shù): 

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

字符串函數(shù):

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

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

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

加密函數(shù): 

select md5('123456'); 

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

演示MySQL中流程控制語句 

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

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

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; 

將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數(shù)據(jù)庫必知必會sql語句(加強版),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

  • Mysql數(shù)據(jù)庫時間查詢舉例詳解

    Mysql數(shù)據(jù)庫時間查詢舉例詳解

    在項目開發(fā)中,一些業(yè)務表字段經(jīng)常使用日期和時間類型,而且后續(xù)還會牽涉到這類字段的查詢,下面這篇文章主要給大家介紹了關(guān)于Mysql數(shù)據(jù)庫時間查詢的相關(guān)資料,文中通過圖文介紹的非常詳細,需要的朋友可以參考下
    2023-05-05
  • Navicat連接遠程服務器里docker中mysql的方法(已解決)

    Navicat連接遠程服務器里docker中mysql的方法(已解決)

    相信大家都有在遠程服務器上進行開發(fā)吧,其中MySQL的使用率應該也會挺高,這篇文章主要給大家介紹了關(guān)于Navicat連接遠程服務器里docker中mysql的相關(guān)資料,需要的朋友可以參考下
    2024-04-04
  • MySQL通過binlog恢復數(shù)據(jù)

    MySQL通過binlog恢復數(shù)據(jù)

    通過了解binlog日志的相關(guān)配置,簡單掌握通過binlog對數(shù)據(jù)庫進行數(shù)據(jù)恢復操作。有此需求的朋友可以參考下
    2021-05-05
  • 帶例子詳解Sql中Union和Union?ALL的區(qū)別

    帶例子詳解Sql中Union和Union?ALL的區(qū)別

    這篇文章主要介紹了帶例子詳解Sql中Union和Union?ALL的區(qū)別,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-09-09
  • MySQL非空約束(not null)案例講解

    MySQL非空約束(not null)案例講解

    這篇文章主要介紹了MySQL非空約束(not null)案例講解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • MySQL中VARCHAR與CHAR格式數(shù)據(jù)的區(qū)別

    MySQL中VARCHAR與CHAR格式數(shù)據(jù)的區(qū)別

    char是一種固定長度的類型,varchar則是一種可變長度的類型,那么他們具體使用過程中有什么區(qū)別嗎
    2015-09-09
  • IP處理函數(shù)inet_aton()和inet_ntoa()使用說明

    IP處理函數(shù)inet_aton()和inet_ntoa()使用說明

    IP處理函數(shù)inet_aton()和inet_ntoa()使用說明,需要的朋友可以參考下
    2012-03-03
  • MySQL性能優(yōu)化之一條SQL在MySQL中執(zhí)行的過程詳解

    MySQL性能優(yōu)化之一條SQL在MySQL中執(zhí)行的過程詳解

    天天和數(shù)據(jù)庫打交道,一天能寫上幾十條 SQL 語句,但你知道系統(tǒng)是如何和數(shù)據(jù)庫交互的嗎?下面這篇文章主要給大家介紹了關(guān)于MySQL性能優(yōu)化之一條SQL在MySQL中執(zhí)行的過程的相關(guān)資料,需要的朋友可以參考下
    2023-02-02
  • MySQL rand函數(shù)實現(xiàn)隨機數(shù)的方法

    MySQL rand函數(shù)實現(xiàn)隨機數(shù)的方法

    在mysql中,使用隨機數(shù)寫一個語句能一下更新幾百條MYSQL數(shù)據(jù)嗎?答案是肯定的,使用MySQL rand函數(shù),就可以使現(xiàn)在隨機數(shù)
    2016-09-09
  • MySQL 密碼設(shè)置

    MySQL 密碼設(shè)置

    本文介紹了如何修改一個用戶的密碼,你可以使用三種方法,GRANT語句、SET PASSWORD語句、直接修改授權(quán)表以及使用管理工具mysqladmin。
    2009-04-04

最新評論

河南省| 长武县| 拉萨市| 贵南县| 高雄市| 城固县| 佛教| 张家口市| 察隅县| 元朗区| 冕宁县| 汝南县| 额济纳旗| 平利县| 惠州市| 凌源市| 正蓝旗| 龙陵县| 临清市| 榆社县| 合川市| 塔河县| 阜康市| 临泉县| 淮北市| 怀来县| 沙湾县| 越西县| 苏州市| 九龙县| 陈巴尔虎旗| 郓城县| 收藏| 牟定县| 深州市| 宜兴市| 宣恩县| 东山县| 临武县| 桃园市| 怀来县|