日期:2014-05-17  浏览次数:20847 次

这样的读取数据效果是怎么做的?(数组和随机)
数组和随机函数我一直都没有自己做过,所以再此请教大家了。

我有4张表,这4张表很相似但没有关联,例如都有字段id,   name,   input_time

这样,我从这4张表中各取top   10条数据,
SELECT   TOP   10   [id],[name],[input_time]   FROM   [table]   ORDER   BY   [input_time]   DESC

把这40条数据都存到数组arrS()中

下面使用随机函数,从这40条数据中随机取10条显示出来。

------解决方案--------------------
你可以用临时表的方法

create table table1 (id int primary key, name nvarchar(20), input_time datetime)
insert into table1 (id, name, input_time)
select 1, 'a ', getdate() union all
select 2, 'b ', getdate() union all
select 3, 'c ', getdate() union all
select 4, 'd ', getdate()

create table table2 (id int primary key, name nvarchar(20), input_time datetime)
insert into table2 (id, name, input_time)
select 11, 'aa ', getdate() union all
select 22, 'bb ', getdate() union all
select 33, 'cc ', getdate() union all
select 44, 'dd ', getdate()

create table table3 (id int primary key, name nvarchar(20), input_time datetime)
insert into table3 (id, name, input_time)
select 111, 'aaa ', getdate() union all
select 222, 'bbb ', getdate() union all
select 333, 'ccc ', getdate() union all
select 444, 'ddd ', getdate()


select * into #t from(
select * from table1
union all
select * from table2
union all
select * from table3) as tbltemp

select * from #t order by newid()

drop table #t

------解决方案--------------------
这样直接insert into 一个字符串入临时表即可.不用专门在表中加表名字段.
SELECT TOP 3 [id],realty,room,hall,toilet,realty_type,input_time, 'house_together ' FROM house_together ORDER BY [id] DESC

ttype也一样,没有的,你加个空值即可.