SQL语句汇总
SQL :Structured Query Language结构化查询语言
1.Select [Predicate] *(filed) from table/view Where ... Group by ... Having... Order by ... With ...
Predicate:包括all/Distinct/Distinctrow/Top,限制查询结果;
As可以命名别名;
Where ... 指定某些条件,将所有符合条件的记录过滤出来,下面是SQL提供的运算符和关键字。
算术运算符:+、-、*、/、%
比较运算符:=、<、>、>=、<=、<>
字符串操作比较符:like、 not like
逻辑操作符:and、or、not
值的域:Between、not between
值的列表:in,not in
未知的值:is null、is not null
Group by ... 多和聚合函数一起使用,sum、avg、count、max、min、first、last。
Having... 组或聚合函数的条件判断
2.高级查询
Union合并多个结果集。
Inner join内联接查询。
Outer join外联接查询。(Left/Right Outer join)
Trasform交叉表查询。
例:Trasform sum(销量) as 销量 select 语言类型 from 图书销售 group by 语言类别 pivot 销售时间
Case静态交叉表。(Case ... When ... Then ..else null end) as [...]
用存储过程实现动态交叉表。
3.其他
格式化函数Format(参数,格式) ---不支持SQL server
字符串函数:Mid、Len
日期函数:DateDiff
相关文档:
经典SQL语句
1、说明:复制表(只复制结构,源表名:a 新表名:b) (Access可用)
法一:select * into b from a where 1<>1
//a必须是已经存在的表,但是b可以不存在,当b不存在时,系统会自己创建表b,该方法只会复制表的结构,而不会复制表的数据
法二:select top 0 * int ......
SQL:
using System.Data.SqlClient;
string sql = "server=.;uid=sa;pwd=;database=tablename;";
ACCESS:
using System.Data.OleDb;
string sql = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA Source=" + HttpRuntime.AppDomainAppPath + "//App_Data//db.mdb";
HttpRuntime.AppDomainAppPath 为根目录
......
windows 集成验证:
<connectionStrings>
<add name="ConnectionStr" connectionString="Data Source=CAIPENG-PC;database=Test;Integrated Security=SSPI" providerName="System.Data.SqlClient"/>
</connectionStrings>
或者
<connectionStrings>
<add name=" ......
--得到数据库的所有者名称
SELECT distinct RDB$OWNER_NAME AS DATABASE_OWNER
from RDB$RELATIONS
WHERE (RDB$SYSTEM_FLAG = 1);
--根据表名得到表的主键
SELECT RC.RDB$CONSTRAINT_NAME AS CONSTRAINT_NAME,
I.RDB$RELATION_NAME AS TABLE_NAME,
S.RDB$FIELD_NAME AS COLUMN_NAME
from RDB$RELATION_CONSTRAINTS ......