'SQL语句
"SELECT username FROM opinion,opinion1"
"SELECT username FROM opinion,opinion1 WHERE opinion.id=opinion1.id"'两个表链接,只显示符合条件的结果
"SELECT username FROM opinion JOIN opinion1 ON opinion.id=opinion1.id"'同上
"SELECT username FROM opinion LEFT OUTER JOIN opinion1 ON opinion.id=opinion1.id"'两个表链接,显示主表opinion的全部和从表opinion1符合条件的结果
"SELECT username FROM opinion RIGHT OUTER JOIN opinion1 ON opinion.id=opinion1.id"'同上,上面为左链接,此语句为右链接
'多个表
"SELECT opinion.username as username FROM opinion,opinion1"
'多个表的记录输出,表opinion的username显示为username。
"SELECT TOP [number] username FROM opinion"
'只显示前几个记录,数量由[number]指定
"SELECT DISTINCT username FROM opinion"
'查询结果不重复
"WHERE vote is null"
'值是否为空
"WHERE vote BETWEEN 7 AND 10"
'值在7到10之间,包括7和10
"WHERE vote IN (1,10)"或"WHERE vote IN ('lihao','wangliu')"或"WHERE vote IN (select username from user where sex='男')"
'注意表达式IN 的使用。这个SELECT 语句只取出vote的值等于括号中的值之一的记录。
"WHERE site_desc LIKE '%trading cark%'"
'site_desc中包含有trading cark的,"%"百分号通配符指零与多个字符、"_"通配符指一个字符
"WHERE site_desc LIKE '[N-Z]%'"
'最后,通过使用下划线字符(_),你可以匹配任何单个字符。
"WHERE site_desc LIKE '[^Y]%"
'要得到那些名字不以Y开头的^为非的意思
"WHERE site_desc LIKE '%[%]%"
'语句返回所有其描述中包含百分号的
"GROUP BY site_desc"
'以列名分组
"HAVING site_desc='home'"
'以列名分组中的条件语句
"select user from user where user='李一'
UNION
select user from user where user='张二'
"
'结果连接语句
"SELECT AVG(username) as username FROM opinion"
'求平均数
"SELECT COUNT(username) as username FROM opinion"
'求统计个数
"SELECT COUNT(DISTINCT username) as username FROM opinion"
'求统计个数(当username出现2次以上,只记1次)
"SELECT COUNT(*) FROM opinion"
'求统计个数(count一般不统计空值,上面的可以包含空值)
"SELECT SUM(username) as username FROM opinion"
'求和
"SELECT MAX(username) as username FROM opinion"
'求最大值
"SELECT MIN(username) as username FROM opinion"
'求最小值
"SELECT RTRIM(username),LTRIM(user) FROM opinion"
'空格删除(LTRIM删除左侧的,RTRIM删除右侧的)
"UPDATE mytable SET first_column=GETDATE() WHERE second_column=’Update Me!’"
'first_column赋值为当前时间(LTRIM删除左侧的,RTRIM删除右侧的)
"SELECT DATEPART(mm,dateandtime) as username FROM opinion"
'提取时间字段的单个值
'日期部分 简写 值
'year yy 1753-9999
'quarter qq 1-4
'month mm 1-12
'day of year dy 1-366
'day dd 1--31
'week wk 1--53
'weekday dw 1--7(Sunday--Saturday)
'hour hh 0--23
'minute mi 0--59
'second ss 0--59
'milisecond ms 0--999
"SELECT DATENAME(mm,dateandtime) as username FROM opinion"
'提取时间字段的单个值,值为英文字符串
"SELECT entrydate ‘Time Entered’DATEDIFF(hh,entrydate,GETDATE()) ‘Hours Ago’ FROM weblog"
'返回entrydate与当前时间的小时差(前面的时间是过去时,后面的时间时将来时,返回值为正数)
"SELECT entrydate ‘Time Entered’DATEADD(mm,1,firstvisit_date) ‘Hours Ago’ FROM weblog"
'返回entrydate加一个月的时间(正数的时间向后,负数的时间向前)
"SELECT * FROM opinion ORDER BY user DESC"
'排序,降序(ASC为升序)
"INSERT INTO mytable (mycolumn) VALUES (‘some data’)"
"DELETE [FROM] {table_name|view_name} [WHERE clause]"
"UPDATE mytable SET first_column=’Updated!’ WHERE second_column=’Update Me!’"
"WHERE (ar_date BETWEEN #"&2005-1-1&"# AND #"&2005-1-31&"#)"
'时间用法