sql基本语句
2013-05-17 15:05
190 查看
1.几个简单的基本的sql语句
选择:select * from table1 where 范围
示例:(_T("select * from MFSupport where BrandID = %d"), iID)
插入一行:insert into table1(field1,field2) values(value1,value2)
示例:(_T("insert into MFSupport(LineType, BandInfo,InnerID), values(\'%s\', \'%s\',%d) "), strLineType, strBandInfo, iInnerID)
删除一行:delete from table1 where 范围
示例:(_T("delete from MFSupport where CfgName = %s and BrandID = %d"), strName, iBrandID)
更新:update table1 set field1=value1 where 范围
示例:(_T("update MfSupport set IsSupport = %d where CfgName = %s"), iNum, strName)
查找:select * from table1 where field1 like ’%value1%’ ---like的语法很精妙,查资料!(待探究!)
(select * from table1 where field1 like ’%雷锋%’ ---like的语法很精妙,查资料!不过会让索引失效,而select * from table1 where field1 like ’雷锋%’ 这句的的话索引是有效的!)
示例:(_T("select * from MFBrand where BrandName like \"%%%s%%\" "), strBrandName)
排序:select * from table1 order by field1,field2 [desc]
总数:select count * as totalcount from table1
求和:select sum(field1) as sumvalue from table1
平均:select avg(field1) as avgvalue from table1
最大:select max(field1) as maxvalue from table1
最小:select min(field1) as minvalue from table1
查询最小值的行:Select * from 表 where
字段 = (Select Min(字段) from 表)
插入一列: alter table table1 add field1 (初始化) comment XXX
示例:(_T("alter table %S add %S int default 0 comment '%s'"), strTableName, strFieldName, strComment)
(_T("alter table %S add %S varchar(0) comment '%s'"), strTableName, strFieldName, strComment)
删除一列: alter table table1 drop field
示例:(_T("alter table %s drop %s"), strTableName, strFileName) //strTableName为表名,strFileName为字段名
查询某一字段列是否存在:
示例:select FieldName from TBL //FieldName为需要查询的字段,TBL为表名
选择:select * from table1 where 范围
示例:(_T("select * from MFSupport where BrandID = %d"), iID)
插入一行:insert into table1(field1,field2) values(value1,value2)
示例:(_T("insert into MFSupport(LineType, BandInfo,InnerID), values(\'%s\', \'%s\',%d) "), strLineType, strBandInfo, iInnerID)
删除一行:delete from table1 where 范围
示例:(_T("delete from MFSupport where CfgName = %s and BrandID = %d"), strName, iBrandID)
更新:update table1 set field1=value1 where 范围
示例:(_T("update MfSupport set IsSupport = %d where CfgName = %s"), iNum, strName)
查找:select * from table1 where field1 like ’%value1%’ ---like的语法很精妙,查资料!(待探究!)
(select * from table1 where field1 like ’%雷锋%’ ---like的语法很精妙,查资料!不过会让索引失效,而select * from table1 where field1 like ’雷锋%’ 这句的的话索引是有效的!)
示例:(_T("select * from MFBrand where BrandName like \"%%%s%%\" "), strBrandName)
排序:select * from table1 order by field1,field2 [desc]
总数:select count * as totalcount from table1
求和:select sum(field1) as sumvalue from table1
平均:select avg(field1) as avgvalue from table1
最大:select max(field1) as maxvalue from table1
最小:select min(field1) as minvalue from table1
查询最小值的行:Select * from 表 where
字段 = (Select Min(字段) from 表)
SELECT * FROM 表 WHERE (字段 IN (SELECT MIN(字段) FROM 表))
插入一列: alter table table1 add field1 (初始化) comment XXX
示例:(_T("alter table %S add %S int default 0 comment '%s'"), strTableName, strFieldName, strComment)
(_T("alter table %S add %S varchar(0) comment '%s'"), strTableName, strFieldName, strComment)
删除一列: alter table table1 drop field
示例:(_T("alter table %s drop %s"), strTableName, strFileName) //strTableName为表名,strFileName为字段名
查询某一字段列是否存在:
示例:select FieldName from TBL //FieldName为需要查询的字段,TBL为表名