sql 语句

1

select * from tbuser where username is not null  不能:username=null

select * from tbuser where username is null

2

select merchanttypeid, COUNT(integral) from tbmerchantinfo

错误:选择列表中的列 'tbmerchantinfo.merchanttypeid' 无效,因为该列没有包含在聚合函数或 GROUP BY 子句中。

select count(integral) from tbmerchantinfo 是可以的.
正确:

select merchanttypeid, COUNT(*) from tbmerchantinfo group by merchanttypeid
select merchanttypeid,sex, COUNT(*) as integral from tbmerchantinfo group by merchanttypeid,sex

按类型和性别分组,分组顺序不分先后.

select merchanttypeid,sex, COUNT(*) as integral from tbmerchantinfo where id<11 group by sex,merchanttypeid

加where条件,表示只在符合条件的记录中进行分组.

select merchanttypeid, COUNT(*),SUM(integral) from tbmerchantinfo group by merchanttypeid having merchanttypeid>1 and sum(integral)>2222 and COUNT(*)>8

结论:对查询进行了分组,则查询字段只能是分组字段和聚合函数,having条件中的也只能是分组字段和聚合函数, having是在前一个分组查询中再进行查询.
分析:

merchanttypeid  registerperponname

  1         张三

  1         李四

对类型分了组,要显示用户名是无法显示的.

3

sql字段的连接:+, 将不同栏位获得的资料串联在一起.

select registerpersonname+'('+loginphone+')' fromtbmerchantinfo

4

主键: id int primary key

外键: 确定资料的完整性. bid int references 表A(aid),

删除表:drop table 表名

清空表中数据:truncate  table 表名

把表B数据导入到表A中:insert into 表A(字段1,字段2,...) select 字段1,字段2,... from  表B  where 条件

5

update T_Person1 set Name=N'中文字符'where Age=20

--中文字符前面最好加上N,以防出现乱码

!=即<>

6

  abs(), 求绝对值 

  ceiling(),"天花板"  舍入到最大整数

  floor(), "地板",  舍入到最小整数

  round(), "半径", 四舍五入

7

  类型转换函数

  CAST(expression as data_type)

  CONVERT(data_type,expression)

8

  空值处理函数ISNULL(expression,value)

  如果expression为空,则返回value,expression不为空, 返回express的值

  select  ISNULL(FName,'佚名')as 姓名  from T_Employee

9

  bit类型只能存储true和false, 即1和0. 

  插入形式:insert into 表名 values('张三','true')或insert into 表名 values('张三', 1)

  同理,查询时也有这两种形式.在数据表存储形式是1和0,而不是true和false.

  注意:用表格形式插入数据时, 只是写1和0, 而不能是true和false.

posted @ 2012-04-01 17:02  pantherbean  阅读(234)  评论(0编辑  收藏  举报