我爱编程

SQL语句范例

2018-04-06  本文已影响0人  gustiness
  1. 检索多个列(SELECT)
SELECT prod_id, prod_name, prod_price
FROM Products;
  1. 检索某列,并排序(ORDER BY)
SELECT prod_id
FROM Products
ORDER BY prod_name;
  1. 检索多个列,按多个列进行排序(ORDER BY)
SELECT prod_id, prod_price, prod_name
FROM Products
ORDER BY prod_price, prod_name;
  1. 指定排序方向(ORDER BY DESC)
SELECT prod_id, prod_name, prod_price
FROM Products
ORDER BY prod_price DESC, prod_name;
  1. 检索多个列,并使用WHERE过滤(WHERE)
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');
  1. 模糊查询(LIKE)
//找出所有以词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;
  1. 计算字段
  1. 函数
  1. 汇总数据(AVG、COUNT、MAX、MIN、SUM)
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;
  1. 分组数据(GROUP BY、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;
  1. 使用子查询
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;
  1. 联结表(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;
上一篇 下一篇

猜你喜欢

热点阅读