在SQL Server中选择具有多个表的查询

| 大家好, 我的SQL Server数据库中有3个表。 1. crossarticle_article具有字段:标题 2. blogs_entries的字段为:title 3. forums_posts。有栏位:主题 现在我想做的是,我在我的网站上进行了搜索,当用户在搜索框中输入任何关键字并点击按钮时,它应该在所有这些表中搜索标题并返回单个结果集。 我如何实现此目标,我正在使用sql server 2008     
已邀请:
就像是:
SELECT \"article\"                  AS type
     , crossarticle_article.id    AS id
     , crossarticle_article.Title AS title_subject
FROM crossarticle_article 
WHERE Title LIKE \"%userinput%\"

UNION ALL

SELECT \"blog entry\"               AS type
     , blogs_entries.id           AS id     
     , blogs_entries.title        AS title_subject
FROM blogs_entries
WHERE title LIKE \"%userinput%\"

UNION ALL 

SELECT \"forum post\"               AS type
     , forums_posts.id            AS id
     , forums_posts.subject       AS title_subject
FROM forums_posts
WHERE subject LIKE \"%userinput%\"
    
UNION
将返回唯一的结果,而
UNION ALL
将返回包括重复项在内的所有内容。
SELECT Title
FROM crossarticle_article
WHERE Title = \'%term%\'
UNION
SELECT Title
FROM blogs_entries
WHERE Title = \'%term%\'
UNION
SELECT subject AS Title
FROM forums_posts
WHERE Title = \'%term%\'
    
SELECT Title
FROM crossarticle_article
WHERE Title = @Title

UNION

SELECT title
FROM blogs_entries
WHERE title = @Title

UNION

SELECT subject
FROM forums_posts
WHERE subject = @Title
    

要回复问题请先登录注册