加上Union All就可以。Select * from 发票单200410 Union All Select * from 发票单200411
同意union all 但是请教 union all会合并相同记录吗?
同意union all 但是如果两个表里有重复记录怎么办?
同意union all 但是请教 union all会合并相同记录吗? ------------------------------------------------------- union all 不会合并相同记录,如果要合并重复记录,用Union
to iuhxq(小灰) union all 相同记录共同(重复)存在 union 相同记录只留一个~~~
--a 建議在後台建一個視圖:視圖內容如下: create view find_发票单 select * from 发票单200410 union all select * from 发票单200411 union all select * from 发票单200412 union all ... select * from 发票单200504 go------b 以後在前台可以用以下語句就可以了: select * from find_发票单 where 號碼='20040101001'--c 以後要加表的話只要修改後台視圖就可以了,與前台查詢語句無關,這樣方便移植.
但是请教 union all会合并相同记录吗? --------union all-----不压缩重复纪录,不排序 union -----合并重复纪录,并排序
谢谢上面几位朋友指点那如果按某个列排序怎么写?order by 列名就可以吗?一定要求两个表的列名相同吧?
我认为可以列名不一样 select a as 列名 from 表1 union select b as 列名 from 表2试试看 union 一样!
用union或union all做一个试图
select * from 发票单200410 union all select * from 发票单200411 這樣不就行了
--a 建議在後台建一個視圖:視圖內容如下: create view find_发票单 select * from 发票单200410 union all select * from 发票单200411 union all select * from 发票单200412 union all ... select * from 发票单200504 go那如何更新视图呢?比如定义了一个包含了从发票单200410-发票单200504的一个视图,到了5月份时候,如何把发票单200505加上去?用什么语句啊?
那就在这 create view find_发票单 select * from 发票单200410 union all select * from 发票单200411 union all select * from 发票单200412 union all ... select * from 发票单200504 go 后面加上 union all select * from 发票单200505 即可完整语句为 create view find_发票单 select * from 发票单200410 union all select * from 发票单200411 union all select * from 发票单200412 union all ... select * from 发票单200504 union all select * from 发票单200505 go
paoluo(一天到晚游泳的鱼)你的意思是每次都把以前的表名都写一遍,好像有些麻烦?能不能如下这样呢? create view find_发票单 select * from fingd_发票单 union all select * from 发票单200505 go???
--这一系列表的名字如果有规律的话就可以实现--假设这些表都是tb开头的,后面加数字declare @sql varchar(8000) set @sql='select * from 'select @sql=@sql+name+' union all select * from ' from sysobjects where name like 'tb%' and xtype='U' order by nameset @sql=left(@sql,len(@sql)-25)exec @sql
--这一系列表的名字如果有规律的话就可以实现--假设这些表都是tb开头的,后面加数字declare @sql varchar(8000) set @sql='select * from 'select @sql=@sql+name+' union all select * from ' from sysobjects where name like 'tb%' and xtype='U' order by nameset @sql=left(@sql,len(@sql)-25)exec(@sql)
lxd99423(苹果),强,真是强,除了set @sql=left(@sql,len(@sql)-25)这个25改成24外,其他都对了,100分里应该得90分了!!!!没有想到这个 select @sql = @sql + name + ........可以返回一下这个结果!就像做了个for 循环一样,而且循环次数又以记录的数量做决定!这是我做梦都没有想到的,佩服!!!!!select * from 進貨單200012 union all select * from 進貨單20011 union all select * from 進貨單20012 union all select * from 進貨單200212 union all select * from 進貨單200310 union all select * from 進貨單20033 union all select * from 進貨單20038 union all select * from 進貨單20039 union all select * from 進貨單20041 union all select * from 進貨單200410 union all select * from 進貨單200411 union all select * from 進貨單200412 union all select * from 進貨單20042 union all select * from 進貨單20043 union all select * from 進貨單20044 union all select * from 進貨單20045 union all select * from 進貨單20046 union all select * from 進貨單20047 union all select * from 進貨單20048 union all select * from 進貨單20049 union all select * from 進貨單20051 union all select * from 進貨單20052 union all select * from 進貨單20053 union all select * from 進貨單20054 union all select * from 進貨單20055 union all select * from 進貨單20073 union all select * from 進貨單20083 union all select * from 進貨單資料
Union All
Select * from 发票单200411
但是请教 union all会合并相同记录吗?
但是如果两个表里有重复记录怎么办?
但是请教 union all会合并相同记录吗?
-------------------------------------------------------
union all 不会合并相同记录,如果要合并重复记录,用Union
union 相同记录只留一个~~~
建議在後台建一個視圖:視圖內容如下:
create view find_发票单
select * from 发票单200410
union all
select * from 发票单200411
union all
select * from 发票单200412
union all
...
select * from 发票单200504
go------b
以後在前台可以用以下語句就可以了:
select * from find_发票单 where 號碼='20040101001'--c
以後要加表的話只要修改後台視圖就可以了,與前台查詢語句無關,這樣方便移植.
--------union all-----不压缩重复纪录,不排序
union -----合并重复纪录,并排序
select a as 列名 from 表1
union
select b as 列名 from 表2试试看
union 一样!
union all
select * from 发票单200411
這樣不就行了
建議在後台建一個視圖:視圖內容如下:
create view find_发票单
select * from 发票单200410
union all
select * from 发票单200411
union all
select * from 发票单200412
union all
...
select * from 发票单200504
go那如何更新视图呢?比如定义了一个包含了从发票单200410-发票单200504的一个视图,到了5月份时候,如何把发票单200505加上去?用什么语句啊?
create view find_发票单
select * from 发票单200410
union all
select * from 发票单200411
union all
select * from 发票单200412
union all
...
select * from 发票单200504
go
后面加上
union all
select * from 发票单200505
即可完整语句为
create view find_发票单
select * from 发票单200410
union all
select * from 发票单200411
union all
select * from 发票单200412
union all
...
select * from 发票单200504
union all
select * from 发票单200505
go
create view find_发票单
select * from fingd_发票单
union all
select * from 发票单200505
go???
set @sql='select * from 'select @sql=@sql+name+' union all select * from ' from sysobjects where name like 'tb%' and xtype='U' order by nameset @sql=left(@sql,len(@sql)-25)exec @sql
set @sql='select * from 'select @sql=@sql+name+' union all select * from ' from sysobjects where name like 'tb%' and xtype='U' order by nameset @sql=left(@sql,len(@sql)-25)exec(@sql)