您好,登錄后才能下訂單哦!
本文主要給大家介紹MySQL內(nèi)外連接及子查詢講析,希望可以給大家補(bǔ)充和更新些知識(shí),如有其它問題需要了解的可以持續(xù)在億速云行業(yè)資訊里面關(guān)注我的更新文章的。
登錄mysql:
mysql -u root -p
輸入密碼:
mysqladmin -u root -p[oldpassword] password newpassword
舊密碼可選,沒有就為空(了解)
使用Navicat_for_MySQL客戶端工具連接mysql
如果無法遠(yuǎn)程連接,請(qǐng)?jiān)谔摍C(jī)中進(jìn)入mysql命令行,輸入如下命令:
GRANT ALL PRIVILEGES ON . TO '用戶名'@'%' IDENTIFIED BY '密碼' WITH GRANT OPTION;
顯示所有的數(shù)據(jù)庫
show databases;
創(chuàng)建數(shù)據(jù)庫fcy
create database fcy default charset=utf8;
刪除數(shù)據(jù)庫
drop database db1
切換到fcy數(shù)據(jù)庫
use fcy;
查看所有的表
show tables;
創(chuàng)建表
create table person_info
(
person_id smallint(5) unsigned auto_increment,
name varchar(50) not null comment '姓名',
country varchar(60) default 'China',
salary decimal(10,2) default 0.00 comment '工資',
primary key (person_id)
)engine=innodb default charset=utf8;
刪除表
drop table person_info123
查看表結(jié)構(gòu)
desc person_info;
插入數(shù)據(jù)
insert into person_info(name, country, salary) values
('xiaoqiang', 'China', 1000.20),
('xiaowang', 'USA', 800.10),
('xiaozhang', 'UK', 300),
('xiaohu', 'Canada', 600.45);
修改表結(jié)構(gòu)--增加字段
alter table person_info add sex VARCHAR(4)
修改表結(jié)構(gòu)--修改字段類型
alter table person_info modify sex INT(4)
alter table person_info change sex sex INT(4)
修改表結(jié)構(gòu)--修改字段名
alter table person_info change sex sex_123 INT(4)
修改表結(jié)構(gòu)--刪除字段
alter table person_info drop sex_123
查詢
SELECT * from person_info where name = 'xiao' and salary = 20
插入
insert into person_info(name, salary) VALUES('xiao', 10)
刪除
DELETE from person_info where salary = 20
修改
UPDATE person_info set salary = 20 where name = 'xiao'
查找
select from person_info where name like '%xiaoqiang%';
select from person_info where name like 'xiao%';
select * from person_info where name like '%qiang';
排序(默認(rèn)升序asc, 降序desc)
select from person_info order by name;
select from person_info order by country desc, salary ASC;
統(tǒng)計(jì)有多少條記錄
select count(*) as totalcount from person_info;
去重
SELECT count(distinct(country)) from person_info;
求和
select sum(salary) as sumvalue from person_info;
平均
select avg(salary) as sumvalue from person_info;
最大
select max(salary) as sumvalue from person_info;
最小
select min(salary) as sumvalue from person_info;
#表連接(內(nèi)連接)
select 表1.列1,表2.列2 from 表1,表2 where 表1.列3 = 表2.列3;
#表外連接
select 表1.列1,表2.列2 from 表1 left join 表2 on 表1.列3 = 表2.列3;
select 表1.列1,表2.列2 from 表1 right join 表2 on 表1.列3 = 表2.列3;
#子查詢
select * from 表1 where 列1 in[=] (select A_id from 表2 where 列2 xxx);
#表連接練習(xí):
新建表A
create table A(
id smallint(5) unsigned auto_increment,
name varchar(50) not null,
primary key(id)
)engine=innodb default charset=utf8;
新建表B
create table B(
id smallint(5) unsigned auto_increment,
address varchar(50) not null,
A_id smallint(5) unsigned,
primary key(id)
)engine=innodb default charset=utf8;
插入數(shù)據(jù)
insert into A (name) values ('zhang'), ('li'), ('wang');
insert into B (address, A_id) values ('beijing',1), ('shanghai',3), ('nanjing',10);
select A.name, B.address from A, B where A.id = B.A_id;
select A.name, B.address from A left join B on A.id = B.A_id;
select A.name, B.address from A right join B on A.id = B.A_id;
select from A where id in (select A_id from B where address='beijing');
select from A where id in (select A_id from B where address='beijing' or address='shanghai');
看了以上關(guān)于MySQL內(nèi)外連接及子查詢講析,希望能給大家在實(shí)際運(yùn)用中帶來一定的幫助。本文由于篇幅有限,難免會(huì)有不足和需要補(bǔ)充的地方,如有需要更加專業(yè)的解答,可在官網(wǎng)聯(lián)系我們的24小時(shí)售前售后,隨時(shí)幫您解答問題的。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。