1. 程式人生 > >mysql常用命令(二)

mysql常用命令(二)

本片文章的重點是子查詢和左連線右連線,順便複習上一篇mysql常用命令(一)的一些命令。

建立之前判斷資料庫是否已經存在,COLLATE utf8_general_ci表示忽略大小寫

CREATE DATABASE IF NOT EXISTS testnewman DEFAULT CHARSET utf8 COLLATE utf8_general_ci;

建立student表

create table student(id int(11) NOT NULL AUTO_INCREMENT,

name varchar(20),

sex varchar(6),

submission_date date,

PRIMARY KEY (id)

 )engine=innodb DEFAULT CHARSET=utf8;

插入資料

insert into student values(1,"小吳","男","2018-10-10");

insert into student values(2,"小張","女","2017-9-10");

insert into student values(3,"小李","女","2016-9-10");

insert into student values(4,"小程","男","2015-9-10");

查詢

select * from student;

select * from student where id =1;

select * from student where sex ='男';

select *from student where id in (2,4);

注意:只有2和4,不是2,3,4

select *from student where id not in (2,4);

起別名

select name as "男" from student where sex ='男';

只顯示第1個

select name as "男" from student where sex ='男' limit 1;

跳過第1個,顯示1個

select name as "男" from student where sex ='男' limit 1,1;

多個篩選條件

select name as "男" from student where sex ='男' and id =4;

select name as "男" from student where sex ='男' or id =4;

select name as "男" from student where submission_date >"2018-01-01";

select name as "男" from student where submission_date <>"2018-01-01";

分組聚合

select count(*),sex from student group by sex ;

select count(*),sex from student where id<3 group by sex ;

select count(*),sex from student where id>1 group by sex having count(*)=1;

建立id表

create table id(id int);

insert into id values(1);

insert into id values(2);

insert into id values(3);

insert into id values(4);

insert into id values(5);

select *from id;

子查詢

select * from student where id in (

select * from id where id>3);

建立A表

create table A(id int not null auto_increment,

 name varchar(20) not null,

 sex varchar(6),

 salary varchar(20),

 department_id int,

 PRIMARY KEY (id)

 )engine=innodb DEFAULT CHARSET=utf8;

create table department(

 id int not null auto_increment,

 department_name varchar(20) not null,

 PRIMARY KEY (id)

)engine=innodb DEFAULT CHARSET=utf8;

向A表中插入資料

insert into A values(1,'jone','male',100,1);

insert into A values(2,'jane','female',60,2);

insert into A values(3,'jason','female',50,3);

insert into A values(4,"Jordan","male",20,10);

向department表中插入資料

insert into department values(1,"Hr");

insert into department values(2,"IT");

insert into department values(3,"OP");

insert into department values(4,"Finance");

內連線:不推薦使用,因為查詢速度會很慢

select person.name,depart.department_name from

A as person inner join department as depart on

person.department_id = depart.id;

或者

select person.name,depart.department_name from

A as person join department as depart on

person.department_id = depart.id;

左連線:列出左邊表中的所有內容,右邊表沒有想關聯的資料會顯示NULL

select person.name,depart.department_name from

A as person left join department as depart on

person.department_id = depart.id;

右連線:列出右邊表中的所有內容,左邊表沒有想關聯的資料會顯示NULL

select person.name,depart.department_name from

A as person right join department as depart on

person.department_id = depart.id;

找到兩個表中的所有欄位

select person.*,depart.* from

A as person right join department as depart on

person.department_id = depart.id;

union(合併排重的功能)

select id from A

union

select id from department;

union all(只合並)

select id from A

union all

select id from department;

select name from A

union all

select department_name from department;