日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > 数据库 >内容正文

数据库

distinct过滤掉重复记录并且显示所有字段_MySQL的所有姿势,我都帮你准备好了...

發(fā)布時(shí)間:2024/7/23 数据库 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 distinct过滤掉重复记录并且显示所有字段_MySQL的所有姿势,我都帮你准备好了... 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

mysql登錄

  • 遠(yuǎn)程登錄方式
  • 本地登陸方式
mysql:mysql -h 主機(jī)名 -P 端口號(hào) -u 用戶名 -p密碼 mysql:mysql -uroot -p密碼

數(shù)據(jù)庫(kù)操作命令

  • 創(chuàng)建數(shù)據(jù)庫(kù)、刪除數(shù)據(jù)庫(kù)、展示所有數(shù)據(jù)庫(kù)名。
  • 查看當(dāng)前數(shù)據(jù)庫(kù)名、查看所有表、查看其他數(shù)據(jù)庫(kù)的表。
  • 查看數(shù)據(jù)庫(kù)的版本、表結(jié)構(gòu)、以及字符集、數(shù)據(jù)庫(kù)引擎。
-- 如果該數(shù)據(jù)庫(kù)不存在,創(chuàng)建該數(shù)據(jù)庫(kù) create database if not exists 數(shù)據(jù)庫(kù)名;-- 設(shè)置指定數(shù)據(jù)庫(kù)的字符集為gbk/uft8 alter database 數(shù)據(jù)庫(kù)名 character set gbk/uft8;-- 如果該數(shù)據(jù)庫(kù)存在刪除該數(shù)據(jù)庫(kù) drop database if exists 數(shù)據(jù)庫(kù)名 ; -- 查看所有數(shù)據(jù)庫(kù) show databases; -- 使用數(shù)據(jù)庫(kù) use 數(shù)據(jù)庫(kù)名; -- 在當(dāng)前數(shù)據(jù)庫(kù)下查看所有表格 show tables; -- 查看其他數(shù)據(jù)庫(kù)的全部表格 show tables from 數(shù)據(jù)庫(kù)名; -- 查看當(dāng)前數(shù)據(jù)庫(kù)名 select database(); -- 查看當(dāng)前MySQl登陸的用戶 select user(); -- 查看當(dāng)前數(shù)據(jù)庫(kù)的版本 select version(); -- 查看表結(jié)構(gòu) desc 表名; -- 查看數(shù)據(jù)庫(kù)的字符集 show variables like '%character%'; show variables like '%char%'; show variables like 'collation%';-- 查看數(shù)據(jù)庫(kù)的引擎 show engines;

表操作命令

  • 創(chuàng)建表、修改表數(shù)據(jù)(插入表數(shù)據(jù)、修改表數(shù)據(jù)、刪除表數(shù)據(jù)、查詢表數(shù)據(jù))。
  • 修改表結(jié)構(gòu)(新增字段、修改表字段、增加主鍵、調(diào)整字段順序、指定位置添加字段)。
  • 根據(jù)原表創(chuàng)建新表(僅復(fù)制表結(jié)構(gòu)、復(fù)制表結(jié)構(gòu)和數(shù)據(jù)、復(fù)制部分表字段和表數(shù)據(jù))。
-- 創(chuàng)建員工表,先在數(shù)據(jù)庫(kù)里面創(chuàng)建該表,為了后面做測(cè)試用 create table dept(department_id int primary key auto_increment, -- 部門編號(hào)dname varchar(14) , -- 部門名字location varchar(13) -- 地址 );create table employee(employee_id int primary key auto_increment, -- 員工編號(hào)c_name varchar(20), -- 員工中文名e_name varchar(20), -- 員工英文名hiredate date, -- 雇傭日期,入職日期salary int, -- 薪水comm int, -- 獎(jiǎng)金job_id int, -- 所屬工種department_id int not null, -- 部門編號(hào)manager_id int -- 直接領(lǐng)導(dǎo)編號(hào) );-- 表中插入數(shù)據(jù) insert into dept values(10,'財(cái)務(wù)部','北京'); insert into dept values(20,'研發(fā)部','上海'); insert into dept values(30,'銷售部','廣州'); insert into dept values(40,'行政部','深圳'); insert into dept values(50,'人力資源','惠州'); -- 表中插入數(shù)據(jù) insert into employee values(1,'劉一','liuyi','1980-12-17',7902,800,1,10,2); insert into employee values(2,'陳二','chener','1981-02-20',7698,1600,3,30,3); insert into employee values(3,'張三','zhangsan','1981-02-22',7698,1250,5,30,4); insert into employee values(4,'李四','lisi','1981-04-02',7839,2975,2,20,5); insert into employee values(5,'王五','wangwu','1981-09-28',7698,1250,1,40,0); insert into employee values(6,'趙六','zhaoliu','1981-05-01',7839,2850,3,50,5);-- 更新數(shù)據(jù): update employee set c_name ="劉一一" where id=1;-- 刪除數(shù)據(jù): delete from employee where id=1;-- 查詢數(shù)據(jù) select * from employee where id in(1,4);-- 修改表名 alter table 舊表名 rename to 新表名;-- 修改表注釋 alter table 表名 comment '系統(tǒng)信息表';-- 修改字段類型和注釋 alter table 表名 modify column 字段名 varchar(20) COMMENT '新的注釋';-- 設(shè)置字段允許為空 alter table 表名 modify column 字段名 varchar(255) null COMMENT '新注釋';-- 增加一個(gè)字段,設(shè)好數(shù)據(jù)類型,且不為空,添加注釋 alert table 表名 add 字段名 varchar(255) not null comment '新注釋'; -- 增加非空、自增主鍵 alter table 表名 add 字段名 int(5) not null auto_increment ,add primary key (aid); -- 修改字段名字(要重新指定該字段的類型) alter table t_app change 原字段名 新字段名 varchar(20) not null;-- 刪除字段 alter table 表名 drop 字段名; -- 在某個(gè)字段后增加字段 alter table 表名 add column 新字段名 int not null default 0 after 字段名;-- 調(diào)整字段順序 alter table employee change num num int not null after departmen_id ; -- 表的刪除 drop table 表名 ;--復(fù)制表的結(jié)構(gòu),不復(fù)制表數(shù)據(jù) create table 新表名 like 舊表名;--復(fù)制表的結(jié)構(gòu),同時(shí)也復(fù)制表數(shù)據(jù) create table 新表名 select * from 舊表名;--只復(fù)制部分表結(jié)構(gòu)和對(duì)應(yīng)的數(shù)據(jù),并且?guī)ШY選條件 create table user1 select id, name,salary from user where salary>3000;--僅復(fù)制部分字段 create table user2 select id, name from user;

表約束

  • NOT NULL: 非空約束
  • DEFAULT: 默認(rèn),用于保證該字段有默認(rèn)值。
  • PRIMARY KEY: 主鍵約束
  • UNIQUE: 唯一約束
  • CHECK: 檢查約束
  • FOREIGN KEY:外鍵約束。
create table student ( id int, name varchar(20) , gender char(1) , seat int, age int, class_id int, PRIMARY KEY(id), -- 主鍵 UNIQUE (seat), -- 唯一鍵 CHECK(gender="男" or gender="女"),-- 檢查 FOREIGN KEY (class_id) REFERENCES class (id) -- 外鍵 )

注釋:這些約束在你創(chuàng)建表和修改表的時(shí)候都可以使用。

表查詢

  • distinct(去重)
  • limit(分頁(yè)查詢)
  • offset(跳過(guò)多少條)
  • UNION 和 UNION ALL(聯(lián)合查詢)
  • like(模糊查詢)
  • where、between、in、or、and條件關(guān)鍵字
  • order by (asc升序、desc降序排序)
  • group by (分組查詢)
  • having 關(guān)鍵字
  • case(流程控制)
-- distinct(去重) select distinct 字段名 from 表名;-- limit(初始記錄行的偏移量是 0(而不是 1),第一個(gè)參數(shù)指定第一個(gè)返回記錄行的偏移量,第二個(gè)參數(shù)指定返回記錄行的最大數(shù)目。) select * from 表名 limit 5,10; -- 檢索記錄行6-15-- offset(跳過(guò)多少條) selete * from employee limit 2 offset 1; +----+----------+-------+--------------+------+ | 2 | lisi | 12000 | 40 | 90 | | 3 | wangwu | 0 | 50 | 0 | +----+----------+-------+--------------+------+ -- 注意: -- 1.數(shù)據(jù)庫(kù)數(shù)據(jù)計(jì)算是從0開始的 -- 2.offset X是跳過(guò)X個(gè)數(shù)據(jù),limit Y是選取Y個(gè)數(shù)據(jù) -- 3.limit X,Y 中X表示跳過(guò)X個(gè)數(shù)據(jù),讀取Y個(gè)數(shù)據(jù)--union 和union all(union all是直接連接,取到得是所有值,記錄可能有重復(fù) union 是取唯一值,記錄沒有重復(fù)。)-- UNION 的語(yǔ)法如下: [SQL 語(yǔ)句 1] UNION [SQL 語(yǔ)句 2]-- UNION ALL 的語(yǔ)法如下: [SQL 語(yǔ)句 1] UNION ALL [SQL 語(yǔ)句 2]-- UNION全連接查詢,把部門表和員工表的所有數(shù)據(jù)都查出來(lái),若有兩個(gè)表都有匹配數(shù)據(jù)的就顯示匹配數(shù)據(jù),若其中有一個(gè)表在另一個(gè)表中沒有匹配數(shù)據(jù)的輸就顯示為null select e.ename,d.dnameFROM employee e left JOIN dept dON e.department_id= d.department_id UNION select e.ename,d.dnameFROM employee e right JOIN dept dON e.department_id= d.department_id; +--------+----------+ | ename | dname | +--------+----------+ | 劉一 | 財(cái)務(wù)部 | | 陳二 | 銷售部 | | 張三 | NULL | | 李四 | 研發(fā)部 | | 王五 | 行政部 | | 趙六 | 人力資源 | +--------+----------+-- 模糊查詢:like,%標(biāo)識(shí)匹配任意哥字符,_表示匹配一個(gè)字符 -- 查詢employee表里面名字含有豪字的員工的全部信息 select * form employee where c_name like '%豪%';-- 查詢員工名中第三個(gè)字母為a,第五個(gè)字母為b的員工信息; select * from employee where c_name like '__達(dá)_法%';-- 當(dāng)查詢的信息信息看里面還有_這樣的特殊字符; select * from employee where c_name like '__%';-- 查詢工資在5000到6000之間的員工信息; select * from employee where salary between 5000 and 6000;-- in、or、and關(guān)鍵字 select * from dept where location in ('北京','上海'); select * from dept where location ='北京' or location ='上海' ; select * from employee where (department_id=30 or department_id=40) and salary >3000;-- 選擇工資不在3000到5000的員工的姓名和工資,按工資降序 select name, salary, department_id from employees where salary not between 3000 and 5000 order by salary desc;-- 查詢每個(gè)部門的員工個(gè)數(shù) select count (*) , department_ id from employee group by department_ id;-- 給30號(hào)部門的增加500,40號(hào)部門增加1000,50號(hào)部門增加1500 select *, (case department_idwhen 30 thensalary+500when 40 thensalary+1000when 50 thensalary+1500else salaryend) '漲后工資' fromemployee;-- 顯示員工的薪資等級(jí) select *, (casewhen salary >=7900 then'高薪資'when salary >=7800 then'中等薪資'when salary >=7700 then'低薪資'else'太難了'end) '薪資等級(jí)' fromemployee order by salary desc;-- 語(yǔ)法格式 select 字段1,字段2,字段3 from 表名 [where 篩選條件] [group by 分組] [having 篩選條件1] [order by 排序列表]

mysql字符串函數(shù)

  • concat():拼接字符串
  • substr():截取字符串
  • instr():獲取子串第一次出現(xiàn)的索引
  • lpad():左邊以指定字符填充到指定長(zhǎng)度
  • rpad():右邊以指定字符填充到指定長(zhǎng)度
  • upper():轉(zhuǎn)換為大寫
  • lover():轉(zhuǎn)換為小寫
  • replace():替換函數(shù)
  • length():獲取字節(jié)長(zhǎng)度
  • trim():去掉字符串前后空格
-- 將英文名全部轉(zhuǎn)換大寫和全部轉(zhuǎn)換為小寫,然后進(jìn)行拼接。 select concat(upper(e_name) ,lower(e_name)) from employee;-- substr,注意:索引從1開始,截取從指定索引處后面所有字符 select substr("歡迎關(guān)注非科班的科班,帶你一起提升代碼內(nèi)功',7) str ;-- 從指定索引截取指定長(zhǎng)度的字符串substr(str,num1,num2)第二個(gè)參數(shù)時(shí)索引、第三個(gè)參數(shù)是指定的長(zhǎng)度。 select substr('歡迎關(guān)注非科班的科班,帶你一起提升代碼內(nèi)功',5,6) str;-- 姓名中首字符大寫,其他字符小寫然后用_拼接,顯示出來(lái) SELECT CONCAT (UPPER (SUBSTR(last_ name,1,1)),'_' ,LOWER (SUBSTR(last_ name,2))) out_put FROM employees;-- instr返回子串在指定字符串第一次出現(xiàn)的素引,如果找不到返回0 select instr('歡迎關(guān)注非科班的科班,帶你一起提升代碼內(nèi)功', '科班') as str;-- length獲取字符串長(zhǎng)度、trim()去掉字符串前后的空字符串 select length(trim(" 非科班的科班 ")) as str; -- lpad用指定的字符實(shí)現(xiàn)左填充指定長(zhǎng)度 select lpad('非科班的科班',9,'*') as str;-- rpad用指定的字符實(shí)現(xiàn)右填充指定長(zhǎng)度 select rpad('非科班的科班',9,'*') as str;-- replace 替換 select replace('非科班的科班', '科班','javaboy') as str;

mysql字符串函數(shù)

  • now:返回當(dāng)前的日期和時(shí)間
  • year:返回年份
  • month:返回月份
  • monthname:以英文形式返回月份
  • day:返回日
  • hour:小時(shí)
  • mimute:分鐘
  • second:秒
  • datediff:返回兩個(gè)日期相差的天數(shù)
  • date_format:將時(shí)間日期轉(zhuǎn)換為字符串
  • str_to_date:將字符轉(zhuǎn)換成日期
  • curdate:返回當(dāng)前日期,不包含時(shí)間
  • curtime:返回當(dāng)前時(shí)間,不包含日期
-- now()返回當(dāng)前系統(tǒng)日期+時(shí)間 select now();-- curdate返回當(dāng)前系統(tǒng)日期,不包含時(shí)間,curtime返回當(dāng)前時(shí)間,不包含日期 select curdate(); select curtime();-- 可以獲取指定的部分,年、月、日、小時(shí)、分鐘、秒 select year(now())年; select year('2020-1-1') 年; select year(hiredate) 年 from employee; select month(now()) 月; select monthname(now()) 月;-- %Y 四位的年份 -- %y 2位的年份 -- %m 2位的月份( 01,02...11,12) -- %c 1位的月份( 1,2...11,12) -- %d 日( 01,02,.. ) -- %H 24小時(shí)制的小時(shí) -- %h 12小時(shí)制的小時(shí) -- %i 分鐘( 00,01...59) -- %s 秒( 00,01...59)-- str_to_date:將字符串轉(zhuǎn)換為指定格式的日期對(duì)象 select str_to_date('2-28-2020','%m-%d-%Y');-- 查詢?nèi)肼毴掌跒?020-2-28的員工信息 select * from employee where hiredate = '2020-2-28' ;-- date_format:將日期轉(zhuǎn)換成字符 select date_format('2020/02/28','%Y年%m月 %d日'); select date_format(now(), '%y年%m月%d日') as date ; -- 查詢工資大于7800的員工的中文名、入職日期 select c_name 中文名, date_format(hiredate,'%m月/%d日 %y年') 入職日期 from employee where salary>7800;

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

  • ceil:中文意思表示天花板,表示向上取整
  • floor:中文意思表示地板,表示向下取整
  • round:對(duì)數(shù)字取四舍五入
  • rand:隨機(jī)取0-1之間的所及小數(shù)
  • mod:取模運(yùn)算
  • truncate:截取,類似于字符串的substr的用法
-- ceil表示向上取整。整數(shù):返回本身。小數(shù):返回的是與該數(shù)相鄰并比該數(shù)大的整數(shù) select ceil(2) ; select ceil(2.21) ; select ceil(-2) ; select ceil(-2.1) ;-- floor向下取整。整數(shù):返回本身。小數(shù):返回的是與該數(shù)相鄰并比該數(shù)大的整數(shù) select floor(2) ; select floor(2.21) ; select floor(-2) ; select floor(-2.1) ;-- rand在0-1之間隨機(jī)去一個(gè)隨機(jī)數(shù) select round(rand()*10) ; --取一個(gè)0-10的隨機(jī)整數(shù)-- round對(duì)數(shù)字取四舍五入 select round(-1.55) ; select round(1.567,2) ;-- mod取余運(yùn)算 select mod(3,2) ; select 3%2;-- truncate截取,第一個(gè)是要截取的數(shù)字,第二個(gè)是要截取的位數(shù),從小數(shù)點(diǎn)后開始算 select truncate(2.3345534,1) ;

創(chuàng)建視圖和索引

  • 視圖view,創(chuàng)建,查詢視圖,刪除視圖
  • 索引index,創(chuàng)建索引,刪除索引
語(yǔ)法格式如下: create view <視圖名> [新字段名1,新字段名2,新字段名3,新字段名4,....] as <select語(yǔ)句>--創(chuàng)建一個(gè)員工的視圖 create view v_employee (id,name,sal,department,hiredate) as select employee_id,c_name,salary,department_id,hiredate from employee ;--查詢視圖 select * from v_employee where sal>7800;--修改視圖 alter view v_employee as select * from employee where salary>7800;--刪除視圖 drop view v_employee ;--基于多表創(chuàng)建視圖 create view v_test as select e.c_name,e.hiredate,e.salary,d.dname,d.location from employee e,dept d where e.department_id=d.department_id;select * from v_test +--------+------------+--------+--------------+----------+ | c_name | hiredate | salary | dname | location | +--------+------------+--------+--------------+----------+ | 劉一 | 1980-12-17 | 7902 | 財(cái)務(wù)部 | 北京 | | 陳二 | 1981-02-20 | 7698 | 銷售部 | 廣州 | | 張三 | 1981-02-22 | 7698 | 銷售部 | 廣州 | | 李四 | 1981-04-02 | 7839 | 研發(fā)部 | 上海 | | 王五 | 1981-09-28 | 7698 | 行政部 | 深圳 | | 趙六 | 1981-05-01 | 7839 | 人力資源 | 惠州 | +--------+------------+--------+--------------+----------+--(1)使用alter table 語(yǔ)句創(chuàng)建索性,使用場(chǎng)景是在表創(chuàng)建完畢之后再添加索引。語(yǔ)法如下: alter table 表名 add 索引類型 (unique,primary key,fulltext,index)[索引名](字段名)-- 普通索引(當(dāng)column_list有多個(gè)的時(shí)候使用逗號(hào)隔開) alter table table_name add index index_name (column_list) ;-- 唯一索引(當(dāng)column_list有多個(gè)的時(shí)候使用逗號(hào)隔開) alter table table_name add unique (column_list) ;--主鍵索引(當(dāng)column_list有多個(gè)的時(shí)候使用逗號(hào)隔開) alter table table_name add primary key (column_list) ;-- (2)使用create index語(yǔ)句對(duì)表增加索引,create index可用于對(duì)表增加普通索引或UNIQUE索引,可用于建表時(shí)創(chuàng)建索引。 create index index_name on table_name(username(length)); -- create只能添加這兩種索引; create index index_name on table_name(column_list); create UNIQUE index index_name on table_name(column_list); create index index_employee on employee(salary,hiredate,c_name);-- (3)刪除索引,刪除索引可以使用ALTER TABLE或DROP INDEX語(yǔ)句來(lái)實(shí)現(xiàn)。 drop index index_name on table_name ; alter table table_name drop index index_name ; alter table table_name drop primary key ;視圖 是一個(gè)虛擬表(非真實(shí)存在),其本質(zhì)是【根據(jù)SQL語(yǔ)句獲取動(dòng)態(tài)的數(shù)據(jù)集,并為其命名】,用戶使用時(shí)只需使用【名稱】即可獲取結(jié)果集,并可以將其當(dāng)作表來(lái)使用,使用視圖時(shí),將其當(dāng)作表進(jìn)行操作即可,由于視圖是虛擬表,所以無(wú)法使用其對(duì)真實(shí)表進(jìn)行創(chuàng)建、更新和刪除操作,僅能做查詢用。 索引 數(shù)據(jù)庫(kù)中將數(shù)據(jù)整齊的排列在磁盤陣列中,當(dāng)獲取數(shù)據(jù)的時(shí)候只需要逐個(gè)搜索,并返回結(jié)果,但是 如果開發(fā)的應(yīng)用有幾百上千萬(wàn)甚至億級(jí)別的數(shù)據(jù),那么不深入了解索引的原理, 寫出來(lái)程序就根本跑不動(dòng),光查一個(gè)數(shù)據(jù)庫(kù)就得好幾天,因此就需要索引,能夠快速的查找的需要的數(shù)據(jù)。

mysql連接查詢

  • 內(nèi)連接:
    • 等值連接
    • 非等值連接
  • 外連接:
    • 左外連接:
    • 右外連接:
  • 自連接
-- 內(nèi)連接 select e.c_name,d.dname,d.location from employee e inner join dept d on e.department_id= d.department_id; +--------+----------+---------+ | c_name | dname | location | +--------+----------+---------+ | 劉一 | 財(cái)務(wù)部 | 北京 | | 陳二 | 銷售部 | 廣州 | | 張三 | 銷售部 | 廣州 | | 李四 | 研發(fā)部 | 上海 | | 王五 | 行政部 | 深圳 | | 趙六 | 人力資源 | 惠州 | +--------+----------+---------+-- 左外連接,是指以左邊的表的數(shù)據(jù)為基準(zhǔn),去匹配右邊的表的數(shù)據(jù),如果匹配到就顯示,匹配不到就顯示為null -- 查詢employee表中的所有數(shù)據(jù)和dept表中與employee中相匹配的數(shù)據(jù),若是沒有匹配的就顯示null select e.c_name,d.dname from employee e left outer join dept d on d.department_id = e.department_id ;-- 修改employee中的數(shù)據(jù) update employee set department_id=60 where employee_id=3;-- 重新查詢,由于dept表中不存在60的數(shù)據(jù),所以再dept表中沒有對(duì)應(yīng)的匹配數(shù)據(jù),顯示為null select e.c_name,d.dname from employee e left outer join dept d on d.department_id = e.department_id ; +--------+----------+ | ename | dname | +--------+----------+ | 劉一 | 財(cái)務(wù)部 | | 陳二 | 銷售部 | | 張三 | NULL | | 李四 | 研發(fā)部 | | 王五 | 行政部 | | 趙六 | 人力資源 | +--------+----------+-- 右外連接和左外連接只不過(guò)是左右表相換也能達(dá)到同樣的效果 -- 這里就是查詢dept部門表對(duì)應(yīng)所有部門和employee表中與之對(duì)應(yīng)的數(shù)據(jù),你會(huì)發(fā)現(xiàn)本來(lái)employee中有6條數(shù)據(jù),只顯示了5條數(shù)據(jù),因?yàn)橛幸粋€(gè)人的部門60再dept中沒有數(shù)據(jù),所以就沒有顯示出來(lái)。 select e.c_name,d.dname from employee e right outer join dept d on d.department_id = e.department_id; +--------+----------+ | ename | dname | +--------+----------+ | 劉一 | 財(cái)務(wù)部 | | 陳二 | 銷售部 | | 李四 | 研發(fā)部 | | 王五 | 行政部 | | 趙六 | 人力資源 | +--------+----------+-- 自連接查詢就是當(dāng)前表與自身的連接查詢,關(guān)鍵點(diǎn)在于虛擬化出一張表給一個(gè)別名 -- 查詢員工以及他的上司的名稱,由于上司也是員工,所以這里虛擬化出一張上司表 select e.c_name 員工名,b.c_name 上司名 from employee e left join employee b on e.manager_id= b.employee_id; +--------+--------+ | 員工名 | 上司名 | +--------+--------+ | 劉一 | 陳二 | | 陳二 | 張三 | | 張三 | 李四 | | 李四 | 王五 | | 王五 | NULL | | 趙六 | 王五 | +--------+--------+

mysql子連接查詢

  • 按子查詢出現(xiàn)在主查詢中的不同位置分
    • select后面:僅僅支持標(biāo)量子查詢。
    • from后面:支持表子查詢。
    • where或having后面:支持標(biāo)量子查詢(單列單行)、列子查詢(單列多行)、行子查詢(多列多行)
    • exists后面(即相關(guān)子查詢):表子查詢(多行、多列)
-- select后面的子查詢 -- 查詢每個(gè)部門員工個(gè)數(shù) SELECT d.*,(SELECT count(*)FROM employee bWHERE b.department_id = d.department_id) AS 員工個(gè)數(shù) FROM dept d;-- 查詢員工號(hào)等于3的部門名稱 SELECT (SELECT a.dname FROM dept a, employee b WHERE a.department_id = b.department_id AND b.employee_id = 3) AS 部門名;-- from后面的子查詢 -- 查詢每個(gè)部門平均工資的工資等級(jí) -- (1)先查詢每個(gè)部門平均工資 SELECTdepartment_id,avg(a.salary) FROM employee a GROUP BY a.department_id;-- (2)然后是查詢薪資等級(jí)表 SELECT * FROM job_grades;-- (3)將上面2個(gè)結(jié)果連接查詢,篩選條件:平均工資 between lowest_sal and highest_sal; SELECTt1.department_id,avg_salary AS '平均工資',t2.grade_level FROM (SELECTdepartment_id,avg(a.salary) avg_salaryFROM employees aGROUP BY a.department_id) t1, job_grades t2 WHEREt1.avg_salary BETWEEN t2.lowest_sal AND t2.highest_sal;-- where和having后面的子查詢 -- 查詢誰(shuí)的工資比javaboy的高? -- (1)查詢lisi的工資 SELECT salary FROM employee WHERE e_name = 'lisi';-- (2)查詢員工信息,滿足salary>上面的結(jié)果 SELECT * FROM employee a WHERE a.salary > (SELECT salaryFROM employeeWHERE e_name = 'lisi');--having后的子查詢 --查詢最低工資大于40號(hào)部門最低工資的部門id和其最低工資 -- (1)查詢40號(hào)部門的最低工資 SELECT min(salary) FROM employee WHERE department_id = 40;--(2)查詢每個(gè)部門的最低工資 SELECTmin(salary),department_id FROM employee GROUP BY department_id;--(3)③在②的基礎(chǔ)上篩選,滿足min(salary)>① SELECTmin(a.salary) minsalary,department_id FROM employee a GROUP BY a.department_id HAVING min(a.salary) > (SELECT min(salary)FROM employeeWHERE department_id = 50);

總結(jié)

以上是生活随笔為你收集整理的distinct过滤掉重复记录并且显示所有字段_MySQL的所有姿势,我都帮你准备好了...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。