mysql自连接查询去重_MySQL命令汇总
數據庫相關操作:
1.數據庫級別: CURD
show databases;
create database db1 default charset=utf8;
use db1;
select database();
drop database db1;
show create database db1;
2.文件級別: CURD
show tables;
create table students(
id int unsigned auto_increment primary key not null,
name varchar(30) not null,
age tinyint unsigend default 0,
height decimal(5,2),
constraint 外鍵名 foreign key (自己列名) references 主表(主列)
gender enmu('男','女','人妖','保密'),
tel int unique not null
)engine=innodb default charset=utf8;
delete from students;
truncate table students;
drop table students;
show create table students;
desc students;
alter table 表名 add 列名 類型 約束;
alter table 表名 modify 列名 新類型 新約束;
alter table 表名 change 原名 新名 新類型 新約束;
alter table 表名 drop 列名;
3.數據級別: CURD
insert?into?students(id,name) values('0','alex'),('0','Eva-J');
insert into students values('0','alex',18,17.5,'女',138888);
delete name from students where id<3;
update students set name='Eva-J' where id=1;
select * from students;
4.去重查詢:
select distinct name,age from students;
select * from students order by age ASC height desc;
5.排序查詢:
select * from students order by age ASC height desc;
6.分頁:
select * from students limit 0,10;
select * from students limit m*(n-1),m;
7.分組:group by 可以做去重查詢。
*****報錯*****select * from students group by gender;
select gender,group_concat(name)?from students group by gender;
select gender,count(*) from students group by gender;
篩選用having
select gender,count(*) from students group by gender having count(*)>3;
8.聚合函數:
select sum(age)?from students;
select count(*) from students;
select avg(age) from students;
select?ifnull(school,'要查找的不存在') from students;
9.連接:
內連接
select * from A表 join B表;----------->笛卡爾積
seleft * from A表 join B表 on?A.gongfuid=B.id;
外連接
****報錯*****select * from A left join B ;
select * from A left join B on A.gongfuid = B.id;
10.自連接:
--創建表--
create table areas(
id varchar(30) not null primary key,
title varchar(30),
pid varchar(30)
);
-- 導入 sql 文件--
source?/路徑.areas.sql
查詢------------>關鍵就是給area表起兩個不同的別名連接。
select * from areas city?join areas pro?on city.pid=pro.id;
11.子查詢:
-標量 求班級中身高高于平均身高的學生信息
select * from students where height > (select avg(height) from students);
-行 求出班級中身高最高并且年齡最大的同學
select * from students where (age,height) = (select max(age), max(height) from students);
-列 查詢所有使用了技能表中技能的所有英雄
select * from hero where gongfuid in (select id from gongfu);
12.數據庫范式:
1NF 一個字段存儲一個數據
2NF =1NF + 表中有主鍵 + 表中非主鍵元素必須完全依賴于主鍵
3NF = 2NF + 非主鍵字段必須直接依賴與主鍵字段(不能傳遞依賴)
13.E-R圖
4種圖形
多對多的時候要建立中間關系表
14.外鍵(唯一)
創建外鍵
表已經存在添加外鍵
alter table 表名 add foreign key(字段名) references 主表名(主表字段);
創建表的同時添加外鍵約束
create table students(
字段名 類型 約束;
....;
forign key (字段名) references 主表名(主表字段)
);
查看外鍵約束名稱
show create table students;
alter table 表名 drop foreign key 外鍵約束名;
15.sql高級語法:
插入另外一個表的查詢語句
insert into 表名(字段名)select...
insert into good_cates(name) select?cate_name from goods group by cate_name;
在創建表的同時插入select 語句數據需要起一樣的名字
create table xxx() select....
create table good_brands (
id int unsigned primary key auto_increment,
name varchar(36) not null)
select brand_name as name?from goods group by brand_name;
連表更新 在連表過程中使用一個標的字段值去修改另外一個表的值
update A join B on 條件 set 字段名=值;
16.參數化列表
sql='insert into xxx values(0,%s,%s,%s)'
course.execute(sql,['參數1','參數2','參數3’])
17.事務
原子性
一致性
隔離性
永久性
Begin?/?start transaction
commit
rollback
命令行默認自動開啟事務自動提交事務
關閉命令行自動提交 ?set autocommit=0;
pymsql默認自動開啟手動提交
conn.commit()
conn.rollback()
一般放在事務中的是insert操作
隱式提交不能撤銷
18.索引
加速查找
show index from 表;
alter table 表名 add index 索引名(字段名(字符串長度));
alter table 表名 add index 索引名(字段名);
alter table 表名 drop 索引名(字段名);
總結
以上是生活随笔為你收集整理的mysql自连接查询去重_MySQL命令汇总的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: arduinohanshu_Arduin
- 下一篇: mysql explain insert