标签:
1. case用法:
--简单case函数 case sex when ‘1‘ then ‘boy‘ when ‘2‘ then ‘girl‘ else ‘其他‘ end; --case搜索函数 case when sex =‘1‘ then ‘boy‘ when sex =‘2‘ then ‘girl‘ else ‘其他‘ end;
举例:判断工资等级,统计每个等级的人数,
SELECT CASE WHEN salary <= 500 THEN ‘1‘ WHEN salary > 500 AND salary <= 600 THEN ‘2‘ WHEN salary > 600 AND salary <= 800 THEN ‘3‘ WHEN salary > 800 AND salary <= 1000 THEN ‘4‘ ELSE NULL END salary_class, COUNT(*) FROM Table_A GROUP BY CASE WHEN salary <= 500 THEN ‘1‘ WHEN salary > 500 AND salary <= 600 THEN ‘2‘ WHEN salary > 600 AND salary <= 800 THEN ‘3‘ WHEN salary > 800 AND salary <= 1000 THEN ‘4‘ ELSE NULL END;
2. sum()与count()区别:
cout()是返回匹配条件的行数。
cout(column_name)函数返回指定列的值得数目(null不计入):
select count(column_name) from table_name
cout(*)函数返回表中的记录数:
select count(*) from table_name;---返回table表中所有的记录(记录也就是一条数据,即行,一条数据包含多个数据,每个数据就是一个字段)
sum()函数
sum函数返回数值列的总数(总额)
select sum(coulum_name) from table_name,
例如:
select sum(orderprice) as ordertotal from orders;
功能是查找"orderprice"该字段的总数。--也就是该字段下的所有值,
*:没有sum(*)这种方法。
3. select into 与insert into select区别:
insert into table2 (feild1,feils2,..) select value1,value2,.. from table1;
要求目标表table2必须存在,由于目标表table2已经存在,所以除了插入源表table1的字段外,还可以插入常量。
如:Insert into Table2(a, c, d) select a,c,5 from Table1
select into from :
select vae1,val2 into table2 from table1
要求目标表table2不存在,因为在插入时会自动创建表table2,并将table1中指定字段数据复制到table2。
参考:http://www.cnblogs.com/freshman0216/archive/2008/08/15/1268316.html
4. order by 与group by 用法
ORDER BY 用在对查询结果进行排序,即查询什么排序相应字段
GROUP BY 用于进行分组排序,与集合函数一起用,举例:
SELECT f_id ,SUM(f_price) AS total_price FROM fruits GROUP BY s_id;
后面必须跟着一个 GROUP BY s_id,如果没有,执行编译时会报错:
ERROR: column "fruits.s_id" must appear in the GROUP BY clause or be used in an aggregate function
标签:
原文地址:http://www.cnblogs.com/space-place/p/5177485.html