您的位置:首页 > 数据库

SQL必知必会(一)

2015-06-17 20:24 260 查看
1、SQL多条语句每句必加分号,多数DB单句不需要加分号,但总是加必定不错。

2、select distinct vend_id from Products;找不同的值

3、select vend_id from Products limit 5 offset 5;从第五行开始找5行数据出来。

4、注释 -- # /**/

5、select prod_name from Products order by prod_name 确保order by是最后一条子句,order by后面依据的列可以是select出来的列,也可以不是,而是表中的其他未select出来的列。

6、select prod_id,prod_price,prod_name from Products order by prod_price,prod_name,先按prod_price排序,再按prod_name排序。

7、select prod_id,prod_price,prod_name from Products order by 2,3这是按查询列的相对位置排序,其中2表示prod_price,3表示prod_name。

8、select prod_id,prod_price,prod_name from Products order by prod_price DESC;按prod_price降序排序。select prod_id,prod_price,prod_name from Products order by prod_price DESC,prod_name;先按prod_price降序拍,再按price_name排序。但是排prod_price由于未制定降序,所以是默认按升序排序的。如果想要多个列都降序排序,则要多个列都加上DESC.ASC是升序排序的意思,但默认是升序的,所以这个基本是没什么用的。

9、select prod_name ,prod_price from Products where prod_price = 3.49;使用where相等检验。

10、其他where子句

select prod_name,prod_price from Products where prod_price < 10;

select prod_name,prod_price from Products where prod_price <= 10;

select prod_name,prod_price from Products where prod_price <> 10;

select prod_name,prod_price from Products where prod_price != 10;

select prod_name,prod_price from Products where prod_price between 5 and 10;包括5和10

select prod_name from Products where prod_price is null;

11、组合where子句

select prod_id,prod_price,prod_name from Products where vend_id = 'DLL01' AND prod_price <= 4; select prod_name,prod_price 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;由于AND比OR优先,所以这一条查出来的是BRS01制造的prod_price大于10的,或者DLL01制造的,无论什么价格的。

可以使用()改变优先级:select prod_name ,prod_price from Products where (vend_id = 'DLL01' OR vend_id = 'BRS01') AND prod_price >= 10;

12、select prod_name,prod_price from Products where vend_id IN ('DLL01','BRS01') order by prod_name;IN和OR的功能相当,但是性能更好,也可以包含其他SELECT语句。

13、select prod_name from Products where not vend_id = 'DLL01' order by prod_name;可以用<>代替。

14、select prod_id,prod_name from Products where prod_name Like 'Fish%';%通配符,表示任何字符出现任意次数。该句表示找出所有以Fish开头的东西。微软的产品的通配符是*。通配符可以设定是否区分大小。

15、select prod_id,prod_name from Products where prod_name Like '%bean bag%';

16、select prod_name from Products where prod_name like 'F%y';由于列的内容后面可能是空格,所以,原本是Fxxxxy的内容,由于后面有空格而不匹配y,导致检索不出来,可能的解决办法是改成F%y%

17、select prod_id,prod_name,from Products where prod_name like '__ inch teddy bear';下划线只能精确匹配一个字符。

18、select cust_contact from Customers where cust_contact like '[JM]%' order by cust_contact;找出J%或M%,也就是说[]指定的是一个字符集,其中任何一个作为开头都可以。

19、select cust_contact from Customers where cust_contact like '[^JM]%' order by cust_contact;用^可以否定第18条。

20、select Concat(vend_name, vend_country) from Vendors order by vend_name,使用Concat将vend_name和vend_country组合起来成为一个结果。

21、select Concat(RTRIM(vend_name), RTRIM(vend_country)) from Vendors order by vend_name。使用LTRIM、TRIM、RTRIM分别去掉字段左边,左右两边和右边的空格。

22、select Concat(RTRIM(vend_name), RTRIM(vend_country)) As joaner from Vendors order by vend_name。将搜索到的结果命名别名为joaner.

23、select prod_id,quantity,item_price,quantity*item_price As expanded_price from OrderItems Where order_num = 20008;expanded_price是计算出来的结果,SQL支持的计算操作符有+-*/。

24、大多数数据库都支持的函数之文本处理函数:LEFT()、LENGTH()、LOWER()、LTRIM()、RIGHT()、RTRIM()、SOUNDEX()、UPPER()。

25、大多数数据库都支持的函数之日期处理函数:各个数据库都不一样,注意查阅资料,如MySQL是select order_num from Orders where YEAR(order_date) = 2012;

26、大多数数据库都支持的函数之数值处理函数:ABS\COS\EXP\PI\SIN\SQRT\TAN

27、聚集函数,各大数据库基本一致:AVG\COUNT\MAX\MIN\SUM

28、SELECT AVG(prod_price) As avg_price from Products;AVG是返回某列的平均值。

29、select count(*) as num_cust from Customers;count(*)对所有行计数或者对满足条件的行的数目。

30、select max(prod_price) as max_price from Products;;返回一列中的最大值

31、select min(prod_price) as max_price from Products;;返回一列中的最小值

32、select sum(quantity) as items_ordered from OrderItems where order_num = 20005;用于返回指定列的总和。

33、以下两条是组合聚集函数的例子:

select AVG(DISTINCT prod_price) AS avg_price from Products where vend_id = 'DLL01';

select count(*) As num_items,MIN(prod_price) As price_min,MAX(prod_price) As price_max,AVG(prod_price) As price_avg from Products;

34、select vend_id,count(*) as num_prods from Products group by vend_id;按vend_id分组。

35、select vend_id,count(*) as num_prods from Products group by vend_id having count(*) >=4;使用关键字HAVING进行分组过滤。

36、select order_num,count(*) as items from OrderItems group by order_num having count(*) >= 3 order by items,order_num;

37、select cust_id from Orders where order_num IN (select order_num from OrderItems where prod_id = 'RGAN01');这是使用子查询的例子。注意,作为子查询的select语句只能查询单个列,企图检索多个列将返回错误。

38、select cust_name,cust_state,(Select count(*) from Orders where Orders.cust_id = Customers.cust_id) As orders From Customers order by cust_name;由于Orders表和Customers表中都有cust_id字段,所以这里查询时,需要完全限定列名,不然会出错。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: