SQL语句-DQL
SQL语句-DQL
Database Query Language
数据 查询 语言
# show
# 查看数据库
show databases;
# 查看表
show tables;
# 查看指定数据库下的表,不用切换数据库
show tables from wc;
# 查看建库语句
show create database 库名;
# 查看建表语句,也是为了查看注释
show create table 表名;
# 查看建表语句,也是为了查看注释(不用切换数据库)
show create table 库名,表名;
# 查看用户授权语句,也是为了查看该用户的权限
show grants for 用户名@'主机域';
# 查看所有内置变量
show variables;
# 模糊查询
show variables like '%过滤内容';(前面为空也可以查询的到)
# 查看后台执行的SQL语句
show processlist;
# 查看完整的后台执行的SQL语句
show full processlist;
# 查看所有的校验规则
show collation;
# 查看所有字符集与该字符集的默认校验规则
show charset;
# desc
# 查看表结构
desc 库.表
# select
wget http://test.driverzeng.com/MySQL_File/world.sql
root@localhost:(none)> source /root/world.sql
# 查看库
root@localhost:(none)>show databases;
# 进入库
root@localhost:(none)>use world;
# 查看库下的表
root@localhost:world>show tables;
# select常用简单查询
# 查看该表中的所有字段的记录
root@localhost:world>select * from city;
# 查看指定字段的所有数据
root@localhost:world>select name,countrycode from city;
# select行级查询
root@localhost:world>select id,name,countrycode from city limit 120,60;
# select 条件查询
root@localhost:world>select * from city where CountryCode='CHN';
# 查询带CHN和USA的字段
root@localhost:world>select * from city where CountryCode='CHN' or CountryCode='USA';
# 查询CHN和jiangxi的字段
root@localhost:world>select * from city where CountryCode='CHN' and district='jiangxi';
# select模糊查询
# 查询%HN的和JPN的字段
root@localhost:world>select * from city where countrycode like '%HN' or countrycode='JPN';
# select排序查询 order by
# 顺序
root@localhost:world>select * from city where CountryCode='CHN' order by Population;
# 不加条件的
select * from city order by population;
# 倒序
root@localhost:world>select * from city where CountryCode='CHN' order by Population desc;
# 不加条件的
select * from city order by population desc;
# select 范围查询
root@localhost:world>select * from city where Population>10000022;
> < >= <= <> !=
# in 和 or
# 查询CHN和USA字段
root@localhost:world>select * from city where CountryCode in ('CHN','USA');
root@localhost:world>select * from city where CountryCode='CHN' or CountryCode='USA';
# union all 联合查询
root@localhost:world>select * from city where CountryCode='CHN' union all select * fromcity where CountryCode='USA';
# select 分组查询 group by
group by + 聚合函数
聚合函数:
max():最大值
min():最小值
sum():求和
avg():求平均值
count():统计
1.遇到统计想函数
2.形容词前group by
3.函数中央是名词
4.列名select后添加
# 统计世界上每个国家的总人口数
root@localhost:world>select CountryCode,sum(Population) from city group by CountryCode;
# 统计中国各个省的人口数量
root@localhost:world>select District,sum(Population) from city where CountryCode='CHN' group by District;
# 统每个国家的城市数量
root@localhost:world>select CountryCode,count(name) from city group by CountryCode;
字符集设置
操作系统设置字符集
# CentOS6
[root@db01 ~]# source /etc/sysconfig/i18n
[root@db01 ~]# echo $LANG
zh_CN.UTF-8
# CentOS7
[root@db04 tmp]# vim /etc/locale.conf
LANG="en_US.UTF-8"
[root@db04 tmp]# echo $LANG
en_US.UTF-8
建库建表级别设置字符集
# 创建库设置字符集
create database wc charset utf8 collate utf8_general_ci;
# 建表设置字符集
create table test(
id int,
name varchar(10)
) default charset utf8;
# 数据库配置字符集
vim /etc/my.cnf
[mysqld]
character_set_server=utf8