sql server中多表的select查询

发布于 2024-11-01 20:14:26 字数 223 浏览 3 评论 0原文

嘿伙计们, 我的 sql server 数据库中有 3 个表。 1. crossarticle_article 有字段:标题 2. blogs_entries 有字段:标题 3. 论坛帖子。有字段:主题

现在我想做的是,我在我的网站上进行搜索,当用户在搜索框中输入任何关键字并点击按钮时,它应该在所有这些表中搜索标题并返回单个结果集。

我怎样才能做到这一点,我正在使用 sql server 2008

hey guys,
i have 3 tables in my sql server database.
1. crossarticle_article has field: Title
2. blogs_entries has field: title
3. forums_posts. has field: subject

now what i want to do is, i have a search on my website, when user enters any keyword in the searchbox and hit button, it should search for the title in all these table and should return single resultset.

how can i achieve this, i am using sql server 2008

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

倦话 2024-11-08 20:14:26

像这样的东西:

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%"

Something like:

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%"
浅浅淡淡 2024-11-08 20:14:26

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%'

A UNION will return unique results, whereas a UNION ALL will return everything including duplicates.

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%'
昇り龍 2024-11-08 20:14:26
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
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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文