删除数据库,表
DROP DATABASE db_name;
删除表
DROP TABLE table_name;
DROP DATABASE db_name;
删除表
DROP TABLE table_name;
新增表记录insert
1.插入一条记录,对所有字段赋值
INSERT INTO tb_name VALUES (v1,v2,v3,....vn)
2.插入一条记录,对部分字段复制
INSERT INTO
tb_name(c1,c2,c3....,cn) VALUES(V1,V2,V3,...VN);
3.插入多条记录
INSERT INTO tb_name(c1,c2,c3,...,cn)
VALUES(v1,v2,v3,...vn),(v1,v2,v3...,vn),
VALUES(v1,v2,v3,...vn),(v1,v2,v3...,vn),
......;
1.插入一条记录,对所有字段赋值
INSERT INTO tb_name VALUES (v1,v2,v3,....vn)
2.插入一条记录,对部分字段复制
INSERT INTO
tb_name(c1,c2,c3....,cn) VALUES(V1,V2,V3,...VN);
3.插入多条记录
INSERT INTO tb_name(c1,c2,c3,...,cn)
VALUES(v1,v2,v3,...vn),(v1,v2,v3...,vn),
VALUES(v1,v2,v3,...vn),(v1,v2,v3...,vn),
......;
Like模糊查询
select * from member where regname ='nike';
select *from member where regname like 'nik%';
select *from member where regname like 'nik_';
select * from member where regname ='nike';
select *from member where regname like 'nik%';
select *from member where regname like 'nik_';
select * from [ent112].[dbo].[User] where LoginID like '%1%'
like 匹配/模糊匹配,会与 % 和 _ 结合使用。
'%a' //以a结尾的数据
'a%' //以a开头的数据
'%a%' //含有a的数据
'_a_' //三位且中间字母是a的
'_a' //两位且结尾字母是a的
'a_' //两位且开头字母是a的
查询以 java 字段开头的信息。
SELECT * FROM position WHERE name LIKE 'java%';
查询包含 java 字段的信息。
SELECT * FROM position WHERE name LIKE '%java%';
查询以 java 字段结尾的信息。
SELECT * FROM position WHERE name LIKE '%java';
distinct去重
-
DISTINCT: 可选,删除结果集中重复的数据。默认情况下 UNION 操作符已经删除了重复数据,所以 DISTINCT 修饰符对结果没啥影响。
select distinct 字段1,字段2.。。from 表名;
HAVING分组和group by 联合使用,筛选分组后的数据
where子句在聚合前先筛选记录,作用在group by和having子句钱,而having子句在聚合后对组记录进行筛选
对结果进行排序
order by field asc/desc
分页 limit m,n;
select * from member limit 10,10;
从第11条开始取10条记录
取出余额最多的会员,top语法
select * from member order by leaveamount desc limit1;
连接查询inner join on/using
select * from boys inner join girls on boys.matchNum=girls.matchNum;
select * from boys inner join girls using (matchNum);
union all/union 去除重复
MySQL UNION 操作符用于连接两个以上的 SELECT 语句的结果组合到一个结果集合中。多个 SELECT 语句会删除重复的数据。
SELECT country FROM Websites
UNION
SELECT country FROM apps
ORDER BY country;
UNION
SELECT country FROM apps
ORDER BY country;
注释:UNION 不能用于列出两个表中所有的country。如果一些网站和APP来自同一个国家,每个国家只会列出一次。UNION 只会选取不同的值。请使用 UNION ALL 来选取重复的值!
SELECT country FROM Websites
UNION ALL
SELECT country FROM apps
ORDER BY country;
UNION ALL
SELECT country FROM apps
ORDER BY country;
SELECT country, name FROM Websites
WHERE country='CN'
UNION ALL
SELECT country, app_name FROM apps
WHERE country='CN'
ORDER BY country;
WHERE country='CN'
UNION ALL
SELECT country, app_name FROM apps
WHERE country='CN'
ORDER BY country;
UNION 语句:用于将不同表中相同列中查询的数据展示出来;(不包括重复数据)
UNION ALL 语句:用于将不同表中相同列中查询的数据展示出来;(包括重复数据)