1. 程式人生 > 其它 >MySQL七種join理論

MySQL七種join理論

mysql資料庫使用

show databases;  #檢視所有庫
show global variables like '%datadir%';  #檢視庫位置  一般預設在/var/lib/mysql下
use mysql;  #進入mysql庫
select database();  #檢視當前在哪個庫
create database aa;  #建立庫
drop database aa;  #刪除庫

show tables;  #檢視庫中的表
create table bb(id int,name varchar(10),lase varchar(10));  #建立表id,name,lase
drop dable aa,bb  #刪除多個表
describe aa;  desc aa;  #查看錶的結構
show create table aa\G  #查看錶的屬性

alter table aa add phone char(10);  #新增phone列
alter table aa drop phone;  #刪除phone列
rename table aa to cc;  #修改表名

insert into 表名 (列1,列2,列3,,,) values (值1,值2,值3,,,)  #插入資料
insert into aa values (值1,值2,值3,,,)  #插入資料

select * from 表名;  #查看錶中所有資料
select id from aa;  #查看錶中id列
select * from aa where id=1;  #查看錶中id=1這一列
select * from aa where id>1 and id<5;  #查看錶中id大於1小於5的資料

create table aa as select * from cc;  #把cc表複製且建立aa表。
insert into cc select * from aa;  # 把aa表資料複製到cc表,cc表是空的。

delete from aa;  #清空表aa
delete from aa where id=1;  #刪除id=1的列資料

update aa set name='123';  #將aa表中的名字都改為123
update aa set name='123' where id=1;  #將aa表中id=1的名字修改為123

#表連線:有某一個共同的列名
#一個id和name:aa
#一個id和sale:bb

select * from aa join bb where aa.id=bb.id  #檢視兩個表連線
select name,sela from aa join bb where aa.id=bb.id and name='list';  #檢視名字為list的lase
select * from aa join bb using(id);  #兩個表有共同的列