首页 > 代码库 > SQL必知必会

SQL必知必会

1.SQL写成多行的便于阅读,每写完一条记得加;

  select

pro_name

  from 

Products;

2.多个列

SELECT prod_id, prod_name, prod_price
FROM Products;

3.select * from Products;  通配符

4.只返回不同的值 select distinct xxxid from Products;

5.select top5 prod_name from Products;

   选出前五行

   select prod_name from Products    --这是一条注释    /*  */

   limit 5 offset 5; 

从第五行开始列出5个

6.order by确保是最后一句。

  select pro_name 

  from Products

  Order by pro_name;

7.SELECT prod_id, prod_price, prod_name
   FROM Products
   ORDER BY prod_price(2), prod_name(3);

 

技术分享

 

8. 降序  DESC;

DESC关键字只应用到直接位于其前面的列名。在上例中,只对prod_price列指定DESC,对prod_name列不指定。因此,prod_price列以降序排
序,而prod_name列(在每个价格内)仍然按标准的升序排序

9.筛选:

SELECT prod_name, prod_price
FROM Products
WHERE prod_price = 3.49;

 

不等于:!=和<>可以互换

10.SELECT prod_id, prod_price, prod_name
FROM Products
WHERE vend_id = ‘DLL01‘ AND(OR) prod_price <= 4;

11.AND被优先处理;

SELECT prod_name, prod_price
FROM Products
WHERE (vend_id = ‘DLL01‘ OR vend_id = ‘BRS01’)
AND prod_price >= 10;

12.WHERE vend_id = ‘DLL01‘ OR vend_id = ‘BRS01‘ 等价于WHERE vend_id IN(‘DLL01‘,‘BRS01‘);

13.

SELECT prod_name
FROM Products
WHERE NOT (<>)vend_id = ‘DLL01‘
ORDER BY prod_name;

14. select prod_name,prod_id

      from Products

      where prod_name like ‘fish%‘;

      搜索以fish开头的所有数据。

    %fish%表示无论什么位置都可以。

‘F%y‘,F开头y结尾

15.

SELECT cust_contact
FROM Customers
WHERE cust_contact LIKE ‘[^JM]%‘
ORDER BY cust_contact;

开头不是J或M的用户

通配符不要过度使用。

16.SELECT vend_name + ‘ (‘ + vend_country + ‘)‘
FROM Vendors
ORDER BY vend_name;     

编辑了(),

SELECT RTRIM vend_name + ‘ (‘ + RTRIM(vend_country) + ‘)‘  AS bobo
FROM Vendors
ORDER BY vend_name;

RTRIM,去掉右边所有空格。

+等于||

作为一个新建的列

17.SELECT prod_id,
quantity,
item_price,
quantity*item_price AS expanded_price
FROM OrderItems
WHERE order_num = 20008;     

quantity和item_price表相乘等到新的列 expanded_price

 

先写到这里吧,我要和珍姐回宿舍了。

SQL必知必会