mysql sql语句总结
select:
1、limit字句
select prod_name from products limit5;使用select语句检索单个列,limit 5 表示返回不多于5行。
select prod_name from products limit 3,4; limit 3,4表示返回从行3开始的4行。第一个数表示开始位置,第二个数表示要检索的行数。
select prod_name from products limit 4 offset 3; 从行3开始取4行。与上句意思一样。
注:数据从行0开始计算。
2、完全限定列名(同时使用表名和列名)
select products.prod_name fromcrashcourse.products;
3、order by字句
select prod_name from products order by prod_name; 表示对prod_name列以字母顺序排序数据
select prod_id,prod_price,prod_name from products order byprod_price,prod_name; 按多个列进行排序,先按价格,若价格相同再按名称
select prod_id,prod_price,prod_name from productsorder by prod_pricedesc;指定排序方向按降序排序
select prod_id,prod_price,prod_name from productsorder byprod_price desc,prod_name;desc只应用直接位于其前面的列名,如果想对多个列进行降序排序,必须为每个列指定desc关键字 先按价格降序排序 再按名字排序
select prod_price from productsorder by prod_price desc limit 1; 使用order by和limit组合,能找出一个列中最高或最低的值。
4、where字句(=、<>、!=、<、<=、>=、between) where过滤的是行,having过滤的是分组
1)select prod_name,prod_price from products where prod_name='fuses'; 检查单个值
2)select vend_id,prod_name from products where vend_id<>1003; 不匹配检查 列出不是1003制造的所有产品
3)select prod_name,prod_price fronm products where prod_price between 5 and 10; 范围值检查
4) select prod_name from products where prod_price is null; 空值检查 返回没有价格的(字段为空)的所有产品
5、组合where字句(and字句和or字句)、in操作符(与or类似)、not操作符
1)select prod_id,prod_price,prod_name from products where vend_id=1003 and prod_price<=10;
2)select prod_id,prod_price,prod_name from products wherevend_id=1002orvend_id=1003;
注:select prod_price,prod_name from products where vend_id=1002 or vend_id=1003 and prod_price >=10;
因为优先处理and ,所以返回结果为由1003制造的任何价格为10美元以上的产品或者由1002制造的所有产品。为避免歧义,可以加括号。
select prod_price,prod_name from products where (vend_id=1002 or vend_id=1003 )and prod_price >=10;返回由1002和1003制造的价格在10美元以上的任何产品。
3)select prod_price,prod_name from products where vend_id in(1002,1003) order by prod_name;
4)select prod_price,prod_name from products where vend_id not in(1002,1003) order by prod_name;
6、like操作符(%、_)
1)select prod_id,prod_name from products where prod_namelike 'jet%'; 返回任意以jet开头的词。
%表示任意字符出现任意多次(0次1次或多次)
2)select prod_id,prod_name from products where prod_name like '_ ton anvil';
_匹配单个字符
7、计算字段
1)拼接字段 contact() 拼接两个列
select Contact(vend_name,'(',vend_country,')')from vendors order by vend_name;
2)rtrim() 去掉右边所有的空格
select Contact(RTrim(vend_name),'(', RTrim(vend_country),')') from vendors order by vend_name;
3)取别名
select Contact(RTrim(vend_name),'(', RTrim(vend_country),')') as vend_title from vendors order by vend_name;
4)执行算术计算
select prod_id,quantity,item_price , quantity*item_price as expanded_price from orderitems where order_num=20005;
8、函数
1)文本处理函数
RTrim()2)日期和时间处理函数
Date()、Time()函数
select cust_id, order_num from orders where Date(order_date)='2005-09-01'; 如果想要的仅是日期,使用Date()函数。 时间用Time()函数。
selectcust_id, order_num from orders where Date(order_date) between '2005-09-01' and '2005-09-30'; 可检索出某一月的订单
3)数值处理函数
9、聚集函数 AVG()、COUNT()、MAX()、MIN()、SUM()
1)avg
select AVG(prod_price) as avg_price from products; 返回products表中所有产品的平均价格
select AVG(prod_price ) as avg_price from products where vend_id=1003;返回特定供应商所提供的产品的平均价格 注:avg()只作用 于某一个列,要获得多个列的平均值,要用多个AVG()函数。
2)count
select count(*) as num_cust from customers; 利用count(*)对所有行计数,不管行中各列有什么值。
select count(cust_email) as num_cust from customers; 利用count(cust_email)对cust_email列中有值的行进行计数,不管行中各列有什么值。
3)max()、 min()函数
select MAX(prod_price)as max_price from products; 返回products中最贵的物品的价格
注:max()一般用于找出最大的数值和日期值。
4)sum()函数
select sum(quantity ) as items_ordered from orderitems where order_num=20005;
5)聚集不同值
只包含不同的值,指定distinct参数。
select AVG(distinct prod_price) as avg_price from products where vend_id=1003;
10、分组数据
1)数组分组 group by
select vend_id,count(*) as num_prods from products group by vend_id;
2)过滤分组 having
select vend_id,count(*) as orders from orders group by cust_id having count(*)>=2;
3)组合使用where和having
select vend_id,count(*) as num_prods from products
where prod_price>=10
group by vend_id
having count(*)>=2;where过滤了所有prod_price 至少为10的行,然后按vend_id分组,having 子句过滤计数为2或2以上的分组。
4)分组和排序
select order_num,SUM(quantity*item_price) as ordertotal from orderitems
group by order_num
having sum(quantity*item_price)>=50
order by ordertotal;
group by子句用来按订单号(order_num列)过滤数据,使得只返回总计订单价格>=50的订单。最后用order by子句排序输出。
11、子查询 (使用在where子句的in操作符)
1)使用子查询 select cust_id from orders
where order_num in(select order_num
from orderitems
where prod_id='TNT2');
14章--18章