2.栏位的资料形态和所填入的资料必须吻合。
3.table_name 也可以是景观 view_name。
insert into table_name (column1,column2,...) select columnx,columny,... from another_table
说明:也可以经过一个子查询(subquery)把别的表格的资料填入。
2、查询资料:
基本查询
select column1,columns2,... from table_name
说明:把table_name 的特定栏位资料全部列出来
select * from table_name where column1 = xxx [and column2 > yyy] [or column3 <> zzz]
说明:
1.'*'表示全部的栏位都列出来。
2.where 之後是接条件式,把符合条件的资料列出来。
select column1,column2 from table_name order by column2 [desc]
说明:order by 是指定以某个栏位做排序,[desc]是指从大到小排列,若没有指明,则是从小到大排列
组合查询
组合查询是指所查询得资料来源并不只有单一的表格,而是联合一个以上的表格才能够得到结果的。
select * from table1,table2 where table1.colum1=table2.column1
说明:
1.查询两个表格中其中 column1 值相同的资料。
2.当然两个表格相互比较的栏位,其资料形态必须相同。
3.一个复杂的查询其动用到的表格可能会很多个。
整合性的查询:
select count (*) from table_name where column_name = xxx
说明:
查询符合条件的资料共有几笔。
select sum(column1) from table_name
说明:
1.计算出总和,所选的栏位必须是可数的数字形态。
2.除此以外还有 avg() 是计算平均、max()、min()计算最大最小值的整合性查询。
select column1,avg(column2) from table_name group by column1 having avg(column2) > xxx
说明:
1.group by: 以column1 为一组计算 column2 的平均值必须和 avg、sum等整合性查询的关键字一起使用。
2.having : 必须和 group by 一起使用作为整合性的限制。
复合性的查询
select * from table_name1 where exists ( select * from table_name2 where conditions )
说明:
1.where 的 conditions 可以是另外一个的 query。
2.exists 在此是指存在与否。
select * from table_name1 where column1 in ( select column1 from table_name2 where conditions )
说明:
1. in 後面接的是一个集合,表示column1 存在集合里面。
2. select 出来的资料形态必须符合 column1。
其他查询
select * from table_name1 where column1 like 'x%'
说明:like 必须和後面的'x%' 相呼应表示以 x为开头的字串。
select * from table_name1 where column1 in ('xxx','yyy',..)
说明:in 後面接的是一个集合,表示column1 存在集合里面。
select * from table_name1 where column1 between xx and yy
上一篇:T-SQL脚本:计算表的宽度
下一篇:SQL中使用WITH AS提高性能



