- 检索多个列(SELECT)
SELECT prod_id, prod_name, prod_price
FROM Products;
- 检索某列,并排序(ORDER BY)
- ORDER BY语句必须是SELECT语句的最后一句,否则会报错。也可以用没有选择的列来排序。
SELECT prod_id
FROM Products
ORDER BY prod_name;
- 检索多个列,按多个列进行排序(ORDER BY)
- 首先会按照价格进行排序,对于价格一样的行,再按照名称排序。
SELECT prod_id, prod_price, prod_name
FROM Products
ORDER BY prod_price, prod_name;
- 指定排序方向(ORDER BY DESC)
- 先对prod_price按照降序,再对prod_name进行升序(默认为升序)。
SELECT prod_id, prod_name, prod_price
FROM Products
ORDER BY prod_price DESC, prod_name;
- 检索多个列,并使用WHERE过滤(WHERE)
- < >或者!= 为不等于
- BETWEEN 为在指定的两个数值之间
- IS NULL 为NULL值
- 单引号用来限制字符串,如果将值和字符串类型的列进行比较,则需要限定引号。用来和数值进行比较的值则不需要引号。
- 可以使用 AND、OR、NOT
SELECT prod_name, prod_price
FROM Products
WHERE prod_price BETWEEN 5 AND 10;
SELECT prod_name
FROM Products
WHERE prod_price IS NULL;
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');
- 模糊查询(LIKE)
- '%'是一种通配符,%表示任何字符出现任意次数(包括匹配0个字符)
- ‘_’总是匹配一个字符,不能多也不能少
- ‘[ ]’可以用来指定一个字符集
//找出所有以词Fish起头的产品
//若改为LIKE ‘%Fish%’ 则表示匹配包含Fish
SELECT prod_id, prod_name
FROM Products
WHERE prod_name LIKE 'Fish%';
//匹配以F起头,以y结尾的所有产品
//有些DBMS会用空格来填补字段的内容,所以结尾就会是空格,将会匹配不出来,解决办法:LIKE ‘F%y%’
SELECT prod_name
FROM Products
WHERE prod_name LIKE 'F%y';
//找出所有以J或者M起头的联系人
SELECT cust_contact
FROM Customers
WHERE cust_contact LIKE '[JM]%'
ORDER BY cust_contact;
- 计算字段
- 连接字符串是,不同DBMS不一样,有的是+,有的是||,MySQL是CONCAT
- RTRIM可以去掉字段的空格
- 可以对字段使用*等操作符,进行数学运算
SELECT CONCAT(vend_name, '(', vend_country, ')')
FROM Vendors
ORDER BY vend_name;
- 函数
- UPPER将文本转换为大写,LTRIM去掉串左边的空格,RTRIM去掉串右边的空格
- 有一些日期相关的函数
- ABS、COS、EXP等数值函数
- 汇总数据(AVG、COUNT、MAX、MIN、SUM)
- AVG忽略列值为NULL的行
- COUNT(*)对表中行的数目进行计数,不管表列中包含的是空值(NULL)还是非空值。使用COUNT(column)对特定列中具有值的行进行计数,忽略NULL值
- MAX、MIN、SUM
- 如果指定列名,则DISTINCT只能用于COUNT()。DISTINCT不能用于COUNT(*)。
SELECT AVG(prod_price) AS avg_price
FROM Products
WHERE vend_id = 'DLL01';
//加了DISTINCT之后,平均值只考虑各个不同的价格
SELECT AVG(DISTINCT prod_price) AS avg_price
FROM Products
WHERE vend_id = 'DLL01';
SELECT SUM(item_price * quantity) AS total price
FROM OrderItems
WHERE order_num = 20005;
- 分组数据(GROUP BY、HAVING)
- WHERE 用来过滤行,HAVING用来过滤分组。WHERE在数据分组前进行过滤,HAVING在数据分组之后进行过滤,所以WHERE排除的行不包括在分组中,这可能会改变计算值,从而影响HAVING子句中基于这些值过滤掉的分组
//先用WHERE子句过滤所有prod_price至少为4的行,然后按照vend_id分组数据,HAVING子句过滤计数为2或2以上的分组。
SELECT vend_id, COUNT(*) AS num_prods
FROM Products
WHERE prod_price >= 4
GROUP BY vend_id
HAVING COUNT(*) >= 2;
- 使用子查询
- 作为子查询的SELECT语句只能查询单个列。企图检索多个列将会返回错误
SELECT cust_id
FROM Orders
WHERE order_num IN (SELCT order_num
FROM OrderItems
WHERE prod_id = 'RGAN01');
SELECT cust_name,
cust_state,
(SELECT COUNT(*)
FROM Orders
WHERE Orders.cust_id = Customers.cust_id) AS
orders
FROM Customers
ORDER BY cust_name;
- 联结表(JOIN)
SELECT vend_name, prod_name, prod_price
FROM Vendors, Products
WHERE Vendors.vend_id = Products.vend_id;
SELECT vend_name, prod_name, prod_price
FROM Vendors INNER JOIN Products
ON Vendors.vend_id = Products.vend_id;
SELECT cust_name, cust_contact
FROM Customers
WHERE cust_id IN (SELECT cust_id
FROM Orders
WHERE order_num IN (SELECT order_num
FROM OrderItems
WHERE prod_id = 'RGAN01'))
也可以使用下面的查询,得到同样的结果
SELECT cust_name, cust_contact
FROM Customers, Orders, OrderItems
WHERE Customers.cust_id = Orders.cust_id
AND OrderItems.order_num = Orders.order_num
AND prod_id = 'RGAN01';
SELECT c1.cust_id, c1.cust_name, c1.cust_contact
FROM Customers AS c1, Customers AS c2
WHERE c1.cust_name = c2.cust_name
AND c2.cust_contact = 'Jim Jones';
SELECT Customers.cust_id, Orders.order_num
FROM Customers LEFT OUTER JOIN Orders
ON Customers.cust_id = Orders.cust_id;
//检索所有客户以及每个客户所下的订单数
SELECT Customers.cust_id, COUNT(Orders.order_num) AS num_ord
FROM Customers INNER JOIN Orders
ON Customers.cust_id = Orders.cust_id
GROUP BY Customers.cust_id;