[MySQL光速入門]005 查詢資料

貓哥的技術部落格發表於2019-04-18

第四章-思維導圖.png

select

資料表三連

  • 檢視錶結構
desc table_name;
複製程式碼
  • 檢視建表語句
show create table table_name;
複製程式碼
  • 檢視錶裡的資料
select * from table_name;
複製程式碼

單表查詢

簡單查詢

  • 查詢所有行和列

    select * from 表名稱;

    select * from book;
    複製程式碼
  • 查詢部分列

    select 欄位名稱1, 欄位名稱2 from 表名稱;

    select `bookid`,`bookname` from book;
    複製程式碼
  • 查詢計算列

    select 欄位名稱1, 欄位名稱2,欄位名稱3 * 欄位名稱4 from 表名稱;

    select `bookid`,`bookname`,bookprice * borrowsum from book;
    複製程式碼
  • 使用別名

    select 欄位名稱1 as 自定義名稱1, 欄位名稱2 as 自定義名稱2, 欄位名稱3 * 欄位名稱4 as 自定義名稱3 from 表名;

    select `bookid` as 圖書ID,`bookname` as 圖書名稱,bookprice * borrowsum as 圖書總價 from book;
    複製程式碼
  • 消除重複行

    select distinct 欄位名稱 from 表名稱;

    select distinct `borrowsum` from book;
    複製程式碼

條件查詢

  • 簡單條件查詢

    select * from 表名稱 where 條件;

    select * from book where borrowsum > 10;
    複製程式碼
  • 複合條件查詢

    select * from book where 條件1 and 條件2;

    select * from book where borrowsum >= 10 and typeid =3;
    複製程式碼
  • 指定範圍查詢

    select * from 表名 where 條件1 and 條件2;

    select * from book where borrowsum >= 10 and borrowsum <= 30;
    複製程式碼

    select * from 表名 where 條件1 or 條件2;

    select * from book where borrowsum < 10 or borrowsum > 30;
    複製程式碼

    select * from 表名 where 欄位名 between 值1 and 值2;

    select * from book where borrowsum between 10 and 30;
    複製程式碼

    select * from 表名 where 欄位 not between 值1 and 值2;

    select * from book where borrowsum not between 10 and 30;
    複製程式碼
  • 指定集合查詢

    select * from 表名 where 欄位 in (值1,值2);

    select * from book where bookname in ('離散數學','Java程式設計');
    複製程式碼

    select * from 表名 where 欄位 not in (值1,值2);

    select * from book where bookname not in ('離散數學','Java程式設計');
    複製程式碼
  • 查詢值為空的行

    select * from 表名 where 欄位名 is null;

    select * from bookborrow where fine is null;
    複製程式碼
  • 模糊查詢

    select * from 表名 where 欄位名 like '欄位值_';

    select * from book where bookname like 'java_';
    複製程式碼

    select * from 表名 where 欄位名 like '%欄位值';

    select * from book where bookname like '%java';
    複製程式碼

    select * from 表名 where 欄位名 like '%欄位值%';

    select * from book where bookname like '%java%';
    複製程式碼

    select * from 表名 where 欄位名 like '欄位值%';

    select * from book where bookname like 'java%';
    複製程式碼

尋找高富帥...

建表語句

drop table if exists `user`;

CREATE TABLE `user` (
  `user_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '使用者主鍵id',
  `user_name` varchar(25) NOT NULL COMMENT '使用者姓名',
  `user_sex` tinyint(1) NOT NULL COMMENT '使用者性別 男生1 女生2',
  `user_height` int(11) NOT NULL COMMENT '使用者身高(cm)',
  `user_weight` int(11) NOT NULL COMMENT '使用者體重(kg)',
  `user_account` double(20,0) NOT NULL COMMENT '使用者賬戶(精確到分)',
  `user_appearance` tinyint(2) NOT NULL COMMENT '顏值, 1-10, 分數越高, 顏值越高',
  PRIMARY KEY (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8;
複製程式碼

插入資料

INSERT INTO `user`(`user_name`, `user_sex`, `user_height`, `user_weight`, `user_account`, `user_appearance`) VALUES ('王思聰', 1, 180, 88, 999900000.00, 6);
INSERT INTO `user`(`user_name`, `user_sex`, `user_height`, `user_weight`, `user_account`, `user_appearance`) VALUES ('陳偉霆', 1, 177, 66, 88880000.00, 9);
INSERT INTO `user`(`user_name`, `user_sex`, `user_height`, `user_weight`, `user_account`, `user_appearance`) VALUES ('鹿晗', 1, 170, 64, 77770000.00, 10);
INSERT INTO `user`(`user_name`, `user_sex`, `user_height`, `user_weight`, `user_account`, `user_appearance`) VALUES ('劉詩詩', 2, 166, 52, 66660000.00, 9);
INSERT INTO `user`(`user_name`, `user_sex`, `user_height`, `user_weight`, `user_account`, `user_appearance`) VALUES ('劉德華', 1, 165, 72,999960000.00, 9);
INSERT INTO `user`(`user_name`, `user_sex`, `user_height`, `user_weight`, `user_account`, `user_appearance`) VALUES ('張藝興', 1, 166, 70,55550000.00, 8);
INSERT INTO `user`(`user_name`, `user_sex`, `user_height`, `user_weight`, `user_account`, `user_appearance`) VALUES ('張嘉倪', 2, 155, 56,44440000.00, 6);
INSERT INTO `user`(`user_name`, `user_sex`, `user_height`, `user_weight`, `user_account`, `user_appearance`) VALUES ('黃子韜', 1, 177, 66,44443333.00, 5);
INSERT INTO `user`(`user_name`, `user_sex`, `user_height`, `user_weight`, `user_account`, `user_appearance`) VALUES ('周筆暢', 2, 160, 54,5550000.00, 4);
INSERT INTO `user`(`user_name`, `user_sex`, `user_height`, `user_weight`, `user_account`, `user_appearance`) VALUES ('陳小紜', 2, 159, 66,67890000.00, 6);
INSERT INTO `user`(`user_name`, `user_sex`, `user_height`, `user_weight`, `user_account`, `user_appearance`) VALUES ('喬振宇', 1, 188,66, 67890000.00, 5);
INSERT INTO `user`(`user_name`, `user_sex`, `user_height`, `user_weight`, `user_account`, `user_appearance`) VALUES ('趙麗穎', 2, 171,50, 77890000.00, 7);
INSERT INTO `user`(`user_name`, `user_sex`, `user_height`, `user_weight`, `user_account`, `user_appearance`) VALUES ('王鷗', 1,  199,55, 3990000.00, 8);
INSERT INTO `user`(`user_name`, `user_sex`, `user_height`, `user_weight`, `user_account`, `user_appearance`) VALUES ('蔡徐坤', 1, 187, 45, 59080000.00, 8);
INSERT INTO `user`(`user_name`, `user_sex`, `user_height`, `user_weight`, `user_account`, `user_appearance`) VALUES ('Angelababy', 2, 177, 55, 45890000.00, 8);
INSERT INTO `user`(`user_name`, `user_sex`, `user_height`, `user_weight`, `user_account`, `user_appearance`) VALUES ('張雲雷', 1, 183,69, 45670000.00, 8);
INSERT INTO `user`(`user_name`, `user_sex`, `user_height`, `user_weight`, `user_account`, `user_appearance`) VALUES ('迪麗熱巴', 2, 155, 55, 458910000.00, 8);
INSERT INTO `user`(`user_name`, `user_sex`, `user_height`, `user_weight`, `user_account`, `user_appearance`) VALUES ('朱一龍', 1, 180, 70, 78950000.00, 6);
INSERT INTO `user`(`user_name`, `user_sex`, `user_height`, `user_weight`, `user_account`, `user_appearance`) VALUES ('鄭爽', 2, 162, 56, 78540000.00, 8);
INSERT INTO `user`(`user_name`, `user_sex`, `user_height`, `user_weight`, `user_account`, `user_appearance`) VALUES ('沈騰', 1, 175, 70, 3890000.00, 7);
INSERT INTO `user`(`user_name`, `user_sex`, `user_height`, `user_weight`, `user_account`, `user_appearance`) VALUES ('雷傑', 1, 179, 70, 34560000.00, 8);
INSERT INTO `user`(`user_name`, `user_sex`, `user_height`, `user_weight`, `user_account`, `user_appearance`) VALUES ('關曉彤', 2, 165, 55, 43440000.00, 6);
INSERT INTO `user`(`user_name`, `user_sex`, `user_height`, `user_weight`, `user_account`, `user_appearance`) VALUES ('許凱', 1, 192,66, 34560000.55, 7);
INSERT INTO `user`(`user_name`, `user_sex`, `user_height`, `user_weight`, `user_account`, `user_appearance`) VALUES ('焦俊豔', 2, 155, 56, 67890000.00, 5);
INSERT INTO `user`(`user_name`, `user_sex`, `user_height`, `user_weight`, `user_account`, `user_appearance`) VALUES ('馬麗', 2, 161, 55, 34560000.00, 7);
複製程式碼

排序

  • desc

    select * from 表名 order by 欄位值 desc;

    select * from book order by bookprice desc;
    複製程式碼
  • asc

    select *from 表名 order by 欄位值 asc;

    select *from book order by bookprice asc;
    複製程式碼
  • 多欄位排序

    select * from 表名 order by 欄位名1 asc, 欄位名2 desc;

    select * from book order by borrowsum asc ,typeid desc;
    複製程式碼

    select * from 表名 order by 欄位名1, 欄位名2 desc;

    select * from book order by borrowsum, typeid desc;
    複製程式碼

限制條數

select * from 表名 limit 查詢個數;

select * from book limit 3;
複製程式碼

select * from 表名 limit 開始位置, 查詢個數;

select * from book limit 0, 3;
複製程式碼
select * from book limit 1, 3;
複製程式碼

聚合函式

  • 最大值(max)

    select max(欄位名) as '自定義名稱' from 表名;

    select max(bookprice) as '最貴的書' from book;
    複製程式碼
  • 最小值(min)

    select min(欄位名) as '自定義名稱...' from 表名;

    select min(borrowsum) as '最受嫌棄的書...' from book;
    複製程式碼
  • 數量(count)

    select count(欄位名) from 表名;

    select count(bookid) from book;
    複製程式碼
  • 總和(sum)

    select sum(欄位名) from 表名;

    select sum(borrowsum) from book;
    複製程式碼
  • 平均值(avg)

    select avg(欄位名) from 表名;

    select avg(bookprice) from book;
    複製程式碼

分組查詢

  • 簡單分組

    select 欄位名1, count(欄位名2) from 表名 group by 欄位名1;

    select borrowsum, count(bookid) from book group by borrowsum;
    複製程式碼
  • 篩選分組結果

    select 欄位1, count(欄位2) from 表名 group by 欄位1 having count(欄位2) = 1;

    select borrowsum, count(bookid) from book group by borrowsum having count(bookid) = 1;
    複製程式碼
  • 分組排序

    select 欄位1, count(欄位2) from 表名 group by 欄位1 order by count(欄位2) desc;

    select borrowsum, count(bookid) from book group by borrowsum order by count(bookid) desc;
    複製程式碼
  • 統計功能分組查詢

    select 欄位1,group_concat(欄位2) from 表名 group by 欄位1 order by 欄位1 desc;

    select borrowsum,group_concat(bookname) from book group by borrowsum order by borrowsum desc;
    複製程式碼
  • 多個分組查詢

    select 欄位1, 欄位2, 聚合函式 from 表名稱 group by 欄位1, 欄位2;

    建表語句以及插入資料

    -- ----------------------------
    -- Table structure for choose_course
    -- ----------------------------
    DROP TABLE IF EXISTS `choose_course`;
    CREATE TABLE `choose_course` (
      `course_name` char(10) DEFAULT NULL,
      `semester_number` int(11) DEFAULT NULL,
      `student_name` char(20) DEFAULT NULL
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    -- ----------------------------
    -- Records of choose_course
    -- ----------------------------
    INSERT INTO `choose_course` VALUES ('語文', '1', '李雷');
    INSERT INTO `choose_course` VALUES ('語文', '1', '韓梅梅');
    INSERT INTO `choose_course` VALUES ('語文', '1', '露西');
    INSERT INTO `choose_course` VALUES ('語文', '2', '莉莉');
    INSERT INTO `choose_course` VALUES ('語文', '2', '格林');
    INSERT INTO `choose_course` VALUES ('數學', '1', '李雷');
    INSERT INTO `choose_course` VALUES ('數學', '1', '名字真難起...');
    複製程式碼

    查詢語句(查詢每門課, 每個學期, 都被多少同學選擇)

    SELECT
    	course_name,
    	semester_number,
    	count('hello')
    FROM
    	choose_course
    GROUP BY
    	course_name,
    	semester_number;
    複製程式碼

多表查詢

908177-20160329123323113-1922028008.jpg

內連線

  • 使用from子句

    SELECT * FROM 表名1, 表名2;

    SELECT * FROM book, reader;
    複製程式碼

    select 表1.欄位1, 表1.欄位2, 表2.欄位1, 表2.欄位2 from 表1,表2;

    select book.bookid,book.bookname,reader.readerid,reader.readername from book,reader;
    複製程式碼
  • 在where中指定連線條件

    SELECT * FROM 表1, 表2 WHERE 表2.欄位 = 表1.欄位;

    SELECT * FROM readertype, reader WHERE reader.retypeid = readertype.retypeid;
    複製程式碼
  • 使用join關鍵字實現連線

    select * from 表1 join 表2 on 表2.欄位 = 表1.欄位;

    select * from readertype join reader on reader.retypeid = readertype.retypeid;
    複製程式碼
  • 為資料表使用別名

    select 表1.欄位1, 表1.欄位2, 表2.欄位1 from 表1 join 表2 on 表2.欄位 = 表1.欄位;

    select readertype.retypeid,readertype.borrowquantity,reader.readerstatus from readertype join reader on reader.retypeid = readertype.retypeid;
    複製程式碼

    select 別名1.欄位1, 別名1.欄位2, 別名2.欄位 from 表1 as 別名1 join 表2 as 別名2 on 別名2.欄位 = 別名1.欄位;

    select a.retypeid,a.borrowquantity,b.readerstatus from readertype as a join reader as b on b.retypeid = a.retypeid;
    複製程式碼
  • 三個表連線查詢

    select 表1.欄位,表2.欄位,表3.欄位 from 表1 join 表2 on 表1.欄位 = 表2.欄位 join 表3 on 表3.欄位 = 表1.欄位;

    select bookborrow.borrowid,bookstorage.bookstatus,reader.readername from bookborrow join bookstorage on bookborrow.bookbarcode = bookstorage.bookbarcode join reader on reader.readerid = bookborrow.readerid;
    複製程式碼

    select 表1.欄位, 表2.欄位, 表3.欄位 from 表1 join 表2 on 表2.欄位 = 表1.欄位 join 表3 on 表3.欄位 = 表2.欄位;

    select bookstorage.bookintime,book.bookname,booktype.typename from bookstorage join book on book.bookid = bookstorage.bookid join booktype on booktype.typeid = book.typeid;
    複製程式碼

    select 表1.欄位, 表2.欄位, 表3.欄位 from 表1 inner join 表2 on 表2.欄位 = 表1.欄位 inner join 表3 on 表3.欄位 = 表2.欄位;

    select bookstorage.bookintime,book.bookname,booktype.typename from bookstorage inner join book on book.bookid = bookstorage.bookid inner join booktype on booktype.typeid = book.typeid;
    複製程式碼

外連線

set foreign_key_checks=0;
drop table if exists `user`;
drop table if exists `user_detail`;
create table user(
	user_id int(11) primary key auto_increment not null,
	user_name varchar(20) not null ,
	user_sex tinyint(1) not null default 1 comment '1 for male 2 for female'
);
create table user_detail(
	user_detail_id int(11) not null primary key auto_increment,
	user_detail_address varchar(255) default '河南平頂山' not null,
	user_detail_phone char(11) not null unique,
	user_detail_uid int(11) not null,
	foreign key(user_detail_uid) REFERENCES user(user_id)
);
insert into user(`user_name`) values('張三');
insert into user(`user_name`) values('李四');
insert into user(`user_name`) values('王五');
insert into user(`user_name`) values('趙六');
insert into user(`user_name`) values('錢七');
insert into user(`user_name`) values('孫八');
insert into user(`user_name`) values('周老九');
insert into user(`user_name`) values('吳老十');
INSERT INTO `user_detail` VALUES (1, '河南平頂山', '15639279531', 1);
INSERT INTO `user_detail` VALUES (2, '河南平頂山', '15639279532', 2);
INSERT INTO `user_detail` VALUES (3, '河南平頂山', '15639279533', 3);
INSERT INTO `user_detail` VALUES (4, '河南平頂山', '15639279534', 4);
INSERT INTO `user_detail` VALUES (11, '河南平頂山', '15639279521', 11);
INSERT INTO `user_detail` VALUES (12, '河南平頂山', '15639279522', 12);
INSERT INTO `user_detail` VALUES (13, '河南平頂山', '15639279523', 13);
INSERT INTO `user_detail` VALUES (14, '河南平頂山', '15639279524', 14);
複製程式碼
  • 左外連線

    select * from 表1 left join 表2 on 表1.欄位 = 表2.欄位;

    select * from user left join user_detail on user.user_id = user_detail.user_detail_uid;
    複製程式碼

    select * from 表2 right join 表1 on 表1.欄位 = 表2.欄位;

    select * from user_detail right join user on user.user_id = user_detail.user_detail_uid;
    複製程式碼
  • 右外連線

    select * from 表1 right join 表2 on 表1.欄位 = 表2.欄位;

    select * from user right join user_detail on user.user_id = user_detail.user_detail_uid;
    複製程式碼
  • 內連線

    select * from 表1 inner join 表2 on 表2.欄位 = 表1.欄位;

    select * from user inner join user_detail on user_detail.user_detail_uid = user.user_id;
    複製程式碼

    select * from 表1 join 表2 on 表2.欄位 = 表1.欄位;

    select * from user join user_detail on user_detail.user_detail_uid = user.user_id;
    複製程式碼

交叉連線

以下兩句, 效果一樣

select * from 表1 cross join 表2 ;

select * from user cross join user_detail ;
複製程式碼

select * from 表1,表2;

select * from user,user_detail;
複製程式碼

以下兩句, 效果一樣

select * from 表1 cross join 表2 on 表2.欄位 = 表1.欄位;

select * from user cross join user_detail on user_detail.user_detail_uid = user.user_id;
複製程式碼

select * from 表1,表2 where 表1.欄位 = 表2.欄位;

select * from user,user_detail where user.user_id = user_detail.user_detail_uid;
複製程式碼

自連線

以下兩句, 效果一樣

select 表別名1.欄位1, 表別名1.欄位2 from 表名 as 表別名1,表名 as 表別名2 where 表別名1.欄位2 > 表別名2.欄位2 and 表別名2.欄位1 = '欄位值' order by 表別名1.`欄位2 desc,表別名1.欄位1

SELECT
	b2.bookname,
	b2.borrowsum 
FROM
	book AS b2,
	book AS b1 
WHERE
	b2.borrowsum > b1.borrowsum 
	AND b1.bookname = '中醫的故事' 
ORDER BY
	b2.borrowsum DESC,
	b2.bookname;
複製程式碼
SELECT
	b2.bookname,
	b2.borrowsum 
FROM
	book AS b2 join
	book AS b1 
on
	b2.borrowsum > b1.borrowsum 
	where b1.bookname = '中醫的故事' 
ORDER BY
	b2.borrowsum DESC,
	b2.bookname;
複製程式碼

select 欄位1,欄位2 from 表名 where 欄位3 > (select 欄位3 from 表名 where 欄位1='欄位值') order by 欄位2 desc, 欄位1;

select bookname,borrowsum from book where bookprice > (select bookprice from book where bookname='中醫的故事') order by borrowsum desc, bookname;
複製程式碼

聯合查詢

select * from 表1 left join 表2 on 表1.欄位 = 表2.欄位 union select * from 表1 right join 表2 on 表1.欄位 = 表2.欄位;

select * from user left join user_detail on user.user_id = user_detail.user_detail_uid union select * from user right join user_detail on user.user_id = user_detail.user_detail_uid;
複製程式碼

子查詢

使用比較運算子的子查詢

  • 查詢價格高於機械設計手冊的書籍的書籍號, 書籍名稱, 書籍單價, 價格從高到低排序
SELECT
	bookid,
	bookname,
	bookprice
FROM
	book 
WHERE
	bookprice > ( SELECT bookprice FROM book WHERE bookname = '機械設計手冊' ) 
ORDER BY
	bookprice DESC;
複製程式碼
  • 查詢類別是學生的讀者資訊, 包括讀者編號, 讀者姓名, 發證日期
SELECT
	readerid,
	readername,
	readerdate 
FROM
	reader 
WHERE
	retypeid = ( SELECT retypeid FROM readertype WHERE typename = '學生' );
複製程式碼

也可以使用連表查詢......

SELECT
	readerid,
	readername,
	readerdate 
FROM
	reader
	JOIN readertype ON readertype.retypeid = reader.retypeid 
	AND typename = '學生';
複製程式碼

[not] in 子查詢

  • 查詢已經借出的書籍id, 書籍名稱
SELECT
	bookid,
	bookname 
FROM
	book 
WHERE
	bookid IN ( SELECT bookid FROM bookstorage WHERE bookstatus = '借出' );
複製程式碼
  • 查詢沒有借出(在館)的書籍id, 書籍名稱
SELECT
	`bookid`,
	`bookname` 
FROM
	`book` 
WHERE
	`bookid` NOT IN ( SELECT `bookid` FROM `bookstorage` WHERE `bookstatus` = '借出' );
複製程式碼
SELECT
	`bookid`,
	`bookname` 
FROM
	`book` 
WHERE
	`bookid` IN ( SELECT `bookid` FROM `bookstorage` WHERE `bookstatus` != '借出' );
複製程式碼

any 子查詢

> any 大於最小的 < any 小於最大的 = any 相當於in();

  • 選擇book表中, 價格大於機械工業出版社最便宜價格的圖書(圖書ID, 圖書名稱, 出版社, 價格)
SELECT
	`bookid`,
	`bookname`,
	`bookpublisher`,
	`bookprice` 
FROM
	`book` 
WHERE
	`bookprice` > ANY ( SELECT bookprice FROM book WHERE bookpublisher = '機械工業出版社' );
複製程式碼

all 子查詢

> all 大於最大的 < all 小於最小的

  • 選擇book表中, 價格大於機械工業出版社最貴价格的圖書(圖書ID, 圖書名稱, 出版社, 價格)
SELECT
	`bookid`,
	`bookname`,
	`bookpublisher`,
	`bookprice` 
FROM
	`book` 
WHERE
	`bookprice` > all ( SELECT bookprice FROM book WHERE bookpublisher = '機械工業出版社' );
複製程式碼

[not] exists子查詢

  • 檢視圖書類別表中沒有圖書的類別id類別名稱
SELECT
	typeid,
	typename 
FROM
	booktype 
WHERE
	NOT EXISTS ( SELECT * FROM book WHERE booktype.typeid = book.typeid );
複製程式碼
  • 檢視圖書類別表中有圖書的類別id類別名稱
SELECT
	typeid,
	typename 
FROM
	booktype 
WHERE
	EXISTS ( SELECT * FROM book WHERE booktype.typeid = book.typeid );
複製程式碼

快速跳轉

相關文章