求一条多表联合查询语句
我想从4个一样结构的表中分别读取一些相关信息几条,并按照所有表统一有的ID进行排序,该怎么实现啊?
例如:5表有ID,TITLE,CONTENT都相同,想用一个存储过程实现查询
我写了一个表的根据他们所共同有的字段选择
create procedure search_other
@typ varchar(50)
as
declare @sql varchar(300)
set nocount on
begin
set @sql= 'select top 2 title,content from news as e where type= ' ' '+@typ+ ' ' ' order by id desc '
exec(@sql)
end
------解决方案--------------------CREATE PROCEDURE search_other
@typ VARCHAR(50)
AS
BEGIN
SET nocount ON
SELECT TOP 2 [id], title,content FROM news WHERE [type]=@typ
UNION
SELECT TOP 2 [id], title,content FROM news1 WHERE [type]=@typ
UNION
SELECT TOP 2 [id], title,content FROM news2 WHERE [type]=@typ
UNION
SELECT TOP 2 [id], title,content FROM news3 WHERE [type]=@typ
UNION
SELECT TOP 2 [id], title,content FROM news4 WHERE [type]=@typ
ORDER BY [id] DESC
END
如果有5表集合的总条件,那就只好写临时表,然后再对临时表操作了