首页 > 代码库 > SQL Server 基础
SQL Server 基础
1.select 查询的列,对列的限制;where 过滤行,对行的限制;group by 集合运算,如取平均数;having 对集合运算取值的限制,如均值要大于30;order by 对排序规则的限制(默认递增)
2.F5执行,效果等同于execute
3.select Top 100 from 表; 查出前100行的数据
4. order by; select id,name,price from Product order by name desc,price; 先按照name递减排序,然后在相同的name中按照price递增排序
select id,name,price from Product order by 2; 按照第二列(即name列)递增排序
5. isnull函数:判断某一数据是否为空;
select id, name, isnull(price ,‘ ‘) from Product order by name desc,price; 为了不困扰用户,如果price为null,则查询出来的结果会为空
6. as关键字:给表列起别名:
select id, name, isnull(price ,‘ ‘) as price123 from Product order by name desc,price; 经过isnull(price ,‘ ‘)操作,查出的原price列没有列名,这时候给该列起个别名price123
7. + 关键字:将“列”与“字符串”连接起来:
select id, name, price, name + ‘产品的价格为’ + convert(varchar,price)from Product order by name desc,price; convert(varchar,price)数据类型转换,将钱类型的price转为varchar类型以方便字符串拼接
8. 算数表达式:+ - * /
select id, name, rate as ‘每小时工资‘, round(rate*40*52,1) as ‘年薪’, round(rate*40*52,0) as ‘年薪’ from Employee; round(rate*40*52,1)小数点后四舍五入保留1位,round(rate*40*52,0)四舍五入不要小数位
select id, name, rate as ‘每小时工资‘, (rate+5)*40*52 as ‘年薪’ from Employee; (rate+5)*40*52改变运算优先级
SQL Server 基础