三連
desc table_name;
複製代碼
show create table table_name;
複製代碼
select * from table_name;
複製代碼
查詢全部行和列java
select * from 表名稱;sql
select * from book;
複製代碼
查詢部分列app
select
字段名稱1
,字段名稱2
from 表名稱;函數
select `bookid`,`bookname` from book;
複製代碼
查詢計算列post
select
字段名稱1
,字段名稱2
,字段名稱3 * 字段名稱4 from 表名稱;ui
select `bookid`,`bookname`,bookprice * borrowsum from book;
複製代碼
使用別名spa
select
字段名稱1
as 自定義名稱1,字段名稱2
as 自定義名稱2, 字段名稱3 * 字段名稱4 as 自定義名稱3 from 表名;設計
select `bookid` as 圖書ID,`bookname` as 圖書名稱,bookprice * borrowsum as 圖書總價 from book;
複製代碼
消除重複行3d
select distinct
字段名稱
from 表名稱;code
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;
複製代碼
使用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 = '學生';
複製代碼
借出
的書籍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
至關於in()
;
機械工業出版社
最便宜價格的圖書(圖書ID, 圖書名稱, 出版社, 價格)SELECT
`bookid`,
`bookname`,
`bookpublisher`,
`bookprice`
FROM
`book`
WHERE
`bookprice` > ANY ( SELECT bookprice FROM book WHERE bookpublisher = '機械工業出版社' );
複製代碼
> all
大於最大的 < all
小於最小的
機械工業出版社
最貴价格的圖書(圖書ID, 圖書名稱, 出版社, 價格)SELECT
`bookid`,
`bookname`,
`bookpublisher`,
`bookprice`
FROM
`book`
WHERE
`bookprice` > all ( SELECT bookprice FROM book WHERE bookpublisher = '機械工業出版社' );
複製代碼
類別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 );
複製代碼