show engines;
show databases;
use `Northwind`;
show tables;
describe `OrderDetails`;
show columns from `OrderDetails`;
show create table `OrderDetails`;
show table status like 'OrderDetails';
show index from `OrderDetails`;
show keys from `OrderDetails`;
select * from information_schema.`TABLE_CONSTRAINTS` where table_schema='northwind';
select * from `Categories`; #商品种类
select * from `Suppliers`; #供应厂商
select * from `Products`; #商品信息
select * from `Customers`; #客户信息
select * from `Employees`; #员工信息
select * from `Shippers`; #货运公司
select * from `Orders`; #订单信息
select * from `OrderDetails`; #订单详情
select * from `Reports`; #报表配置(1)
select `Title` from `Employees`;
select `Title`,`FirstName` from `Employees`;
select `Title` as `职称` from `Employees`; #指定别名
select `Title` `职称`,`FirstName` `名字` from `Employees`; #指定别名,省略as
select * from `Categories` #默认正序
select * from `Categories` order by `CategoryID` asc #正序
select * from `Categories` order by `CategoryID` desc #倒序
select * from `Categories` limit 2 #头2行
select * from `Categories` limit 2,2 #第2行后2行
select * from `Categories` order by `CategoryID` desc limit 2#倒数2行
select count(*) `记录总数` from `Categories`; #计算总数
select `UnitPrice`,`UnitPrice`+10 `结果值` from `OrderDetails`; #查询结果计算
select max(`CategoryID`) from `Categories`; #求一列的最大值
select min(`CategoryID`) from `Categories`; #求一列的最小值
select avg(`UnitPrice`) `平均价格` from `Products`; #求所有商品的平均价格
select avg(`UnitPrice`) from `Products` where `ProductID`<=3; #求指定商品的平均价格
select * from `Categories` where char_length(`CategoryName`)=2; #按所占字符数
select * from `Categories` where length(`PictureFile`)=7; #按所占字节数
select * from `Categories` where `CategoryID`=2;
select * from `Categories` where `CategoryID`<>2;
select * from `Categories` where `CategoryID`!=2;
select * from `Categories` where `CategoryID` in(2,4,6);
select * from `Categories` where `CategoryID` not in(2,4,6);
select * from `Categories` where `CategoryID`>3;
select * from `Categories` where `CategoryID`>=3 and `CategoryID`<6;
select * from `Categories` where `CategoryID`>=3 and `CategoryID`<6 and `CategoryID`<>4;
select * from `Categories` where `CategoryID`<3 or `CategoryID`>6;
select * from `Categories` where `CategoryID`<3 or `CategoryID`>6 or `CategoryID`=5;
select * from `Categories` where `CategoryID` between 3 and 5;
select * from `Categories` where `CategoryID` not between 3 and 5;
select * from `Categories` where `CategoryID` not between 3 and 5 and `CategoryID` not in(1,2);
select * from `Suppliers` where `Fax` is null;
select * from `Suppliers` where `Fax` is not null;
select * from `Categories` where `CategoryName`='谷类/麦片';
select * from `Categories` where `CategoryName` like '_类/麦片';
select * from `Categories` where `CategoryName` like '__类/麦片';
select * from `Categories` where `CategoryName` like '%/麦片';
select * from `Categories` where `CategoryName` like '谷类/%';
select * from `Categories` where `CategoryName` like '%/%';
select * from `Orders` where `OrderDate`='1996-07-04';
select * from `Orders` where `OrderDate`>='1996-01-01' and `OrderDate`<'1997-01-01';
select * from `Orders` where `OrderDate` between '1996-01-01' and '1996-12-31 23:59:59';
MySQL 通配符:
1. %
,包含0个或多个字符的任意字符;
2. _
,任何单个字符。
select distinct `ProductID` from `OrderDetails`; #出现过的ProductID(查询结果不会有重复的值)
select `ProductID`,count(`ProductID`) `订单数量`,sum(`Quantity`) `该类总量` from `OrderDetails` group by `ProductID`; #按ProductID分组,并求得每种的出现次数,与该种类的数量总和
select `ProductID`,count(`ProductID`) 订单数量,sum(`Quantity`) 该类总量 from `OrderDetails` group by `ProductID` having sum(`Quantity`)<200; #在上面分组查询的基础上添加新的条件
select `ProductID`,count(`ProductID`) 订单数量,sum(`Quantity`) `该类总量 from OrderDetails group by ProductID` having sum(`Quantity`)<200 and `ProductID`<>15; #在上面分组查询的基础上添加新的条件
select * from `Products` where `SupplierID` in(select `SupplierID` from `Suppliers` where `City`='上海');
select `Tab1`.`CompanyName` from (select * from `Suppliers` where `City`='上海') as `Tab1`;
select `CompanyName` from (select * from `Suppliers` where `City`='上海') as `Tab1`;
select
`P`.`CategoryID`,
`C`.`CategoryName`,
`P`.`ProductID`,
`P`.`ProductName`,
`P`.`QuantityPerUnit`,
`P`.`UnitPrice`,
`P`.`UnitsInStock`
from `Products` `P`
join `Categories` `C`
on `P`.`CategoryID`=`C`.`CategoryID`;
select `CategoryID`,`CategoryName` from `Categories` where `CategoryID`<=4 union select `CategoryID`,`CategoryName` from `Categories` where `CategoryID`>4; #将两个或两个以上的查询结果合并
select `CategoryID`,`CategoryName` from `Categories` where `CategoryID`<=4 union all select `CategoryID`,`CategoryName` from `Categories` where `CategoryID`>4; #将两个或两个以上的查询结果合并
select concat(`LastName`,`FirstName`) as `姓名`,`TitleOfCourtesy` as `称谓` from `Employees`;
select concat(`LastName`,`FirstName`) `姓名`,case `Gender`
when 0 then '女'
when 1 then '男'
end as `性别` from `Employees`;
select concat(`LastName`,`FirstName`) `姓名`,case `TitleOfCourtesy`
when '女士' then '女孩'
when '先生' then '男孩'
else '未知'
end as `称谓` from `Employees`;
create view `Categories_Products` as select `P`.`CategoryID`,`C`.`CategoryName`,`P`.`ProductID`,`P`.`ProductName`,`P`.`QuantityPerUnit`,`P`.`UnitPrice`,`P`.`UnitsInStock` from `Products` `P` join `Categories` `C` on `P`.`CategoryID`=`C`.`CategoryID`; #创建视图
select * from `Categories_Products`; #查询视图
drop view `Categories_Products`; #删除视图
用视图修改数据表的数据:
update `Categories` set `CategoryName`='牛奶2' where `CategoryID`=2;
update `Categories` set CategoryName='牛奶' where `CategoryID`=2;
update `Categories` set CategoryName='牛奶2',`Description`='暂无描述' where `CategoryID`=2;
delete from `Categories` where `CategoryID`=2;
delete from `OrderDetails` where `OrderID`>10470;
delete from `Categories`; #删除指定表内全部数据:有删除记录,可恢复
truncate `orderdetails`; #删除指定表内全部数据:无删除记录,不可恢复