1.创建数据库
$ mysql -u root -p
CREATE database crashcourse;
USE crashcourse;
SOURCE ~/create.sql;
SOURCE ~/populate.sql;
2.查看数据库状态
SHOW DATALABLES;
SHOW TABLES;
- 显示数据表的属性,属性类型,主键信息,是否为 NULL,默认值等其他信息。
SHOW COLUMNS FROM Products;
- 显示数据表的详细索引信息,包括PRIMARY KEY(主键)
SHOW INDEX FROM Products;
SHOW TABLE STATUS LIKE [FROM db_name] [LIKE 'pattern'] \G:
3.检索数据
SELECT prod_id, prod_name,prod_price
FROM Products;
SELECT * FROM Products;
SELECT DISTINCT vend_id FROM Products;
SELECT vend_id FROM Products LIMIT 5 OFFSET 5; --LIMIT 5, 5
SELECT vend_id FROM Products LIMIT 5 OFFSET 5;
SELECT vend_id FROM Products LIMIT 5 OFFSET 5;
SELECT vend_id FROM Products LIMIT 5 OFFSET 5;
4.排序检查数据
SELECT prod_name FROM Products ORDER BY prod_name;
select prod_id, prod_price, prod_name from Products order by prod_price, prod_name;
select prod_id, prod_price, prod_name from Products order by 2,3;
- 指定排序方向(默认升序ASC,降序需用关键字DESC)
select prod_id, prod_price, prod_name from Products order by prod_price DESC, prod_name;
5.过滤数据
- 使用WHERE子句(关键词WHERE需位于ORDER BY之前)
select prod_price, prod_name from Products where prod_price=3.49 order by prod_name;
select prod_price, prod_name from Products where prod_price<10;
select vend_id, prod_name from Products where vend_id <> "DLL01";
select prod_price, prod_name from Products where prod_price between 5 and 10;
select cust_name from Customers where cust_email is null;
6.高级数据过滤
select prod_id, prod_price, prod_name from Products where vend_id="DLL01" and prod_price <= 4;
select prod_price, prod_name from Products where vend_id = "DLL01" or vend_id = "BRS01";
select prod_name, prod_price from Products where (vend_id = "DLL01" or vend_id = "BRS01") and prod_price >=10;
select prod_name, prod_price from Products where vend_id in ("DLL01", "BRS01") order by prod_name;
select prod_name from Products where not vend_id = "DLL01";
7.用通配符进行过滤
- 通配符只适用于文本字段(字符串),费文本数据类型字段不能使用通配符进行搜索
- 百分号%通配符(搜索区分大小写)
select prod_id, prod_name from Products where prod_name like "Fish%";
where email like "y%@qq.com"
select prod_id, prod_name from Products where prod_name like "__ inch teddy bear";
select prod_id, prod_name from Products where prod_name like "%inch teddy bear";
- 方括号[]通配符(指定字符集,只匹配某一个)
注意[]在MySQL中不用做通配符
[^]用于否定子句中
8.创建计算字段
show columns from Vendors;
select concat(vend_name, " (", vend_country, ")") from Vendors order by vend_name;
- 使用RTRIM()、LTRIM()、TRIM()去除空格,得到格式化数据
- 使用别名(关键词AS)
select concat(vend_name, " (", vend_country, ")") as vend_title from Vendors order by vend_name;插入代码片
select prod_id, quantity, item_price from OrderItems where order_num = 20008;
select prod_id, quantity, item_price, quantity*item_price as expended_price from OrderItems where order_num = 20008;
select now();
select Trim(" abc ");
9.使用函数处理数据
substring();
convert();
curdate();
select vend_name, upper(vend_name) as vend_name_upcase from Vendors order by vend_name;
left()
length()/len()/datalength()
lower()
ltrim()/rtrim()/trim()
right()
soundex()
upper()
select cust_name, cust_contact from Customers where soundex(cust_contact) = soundex("Michael Green");
- 日期和时间处理函数( year() / month() / day() )
select order_num, order_date from Orders where day(order_date)=03;
abs()
cos()
exp()
pi()
sin()
sqrt()
tan()
10.汇总数据
select avg(prod_price) as avg_price from Products;
select avg(prod_price) as avg_price from Products where vend_id = "DLL01";
select count(*) as num_cust from Customers;
select count(cust_email) as num_cust from Customers;
- 最大值max()
用于文本数据时,max()返回按该列排序后的最后一行
max()忽略列值为NULL的行
select max(prod_price) as max_price from Products;
select min(prod_price) as min_price from Products;
select sum(item_price * quantity) as total_price from OrderItems where order_num = 20005;
- 聚集不同值distinct
distinct用于count()时,必须使用列名,不能用于计算或表达式
select avg(distinct prod_price) as avg_price from Products where vend_id = "DLL01";
select count(distinct prod_price) as count_price from Products where vend_id = "DLL01";
select count(*) as num_items,
min(prod_price) as min_price,
max(prod_price) as max_price,
avg(prod_price) as avg_price
from Products;