如何编写一个问题来在SQL中使用关键字从一个表到另一个表来查找结果 [英] How to write a quesr to find a result using Keyword from one table to other in SQL

查看:262
本文介绍了如何编写一个问题来在SQL中使用关键字从一个表到另一个表来查找结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难写一个查询来查找从一个表到另一个表的关键字。



例如,



我有两张桌子。第一张表包含历史记录。



第二张表包含关键字。



如果关键字(多个)在第一个表中包含例如Children或Family等等......然后它应该去找到第二个表中的所有行。



if关键字在第二个表中匹配,然后它应列出第二个表中的所有行。



如何编写查询...?



我想显示结果,当用户登录应用程序然后基于用户关键字时,它应该在表格中找到并且必须显示在gridview中...



如何写这个...







第一桌(关键词表)



关键词用户

儿童101

家庭101

房间102

女孩102

旅馆103

商店104



第二桌(搜索表)



内容

小男孩站着

有一个女孩

他是我的孩子

我的家庭很大

家庭房可用

宿舍已准备好单身汉

商店就在附近



输出



我的参数是User 101.所以关键词是Children和Family。



所以它必须在第二个表格中找到结果。



结果如下。它包含3排



他是我的孩子



我的家庭很大

家庭房可用

I am sturggling to write a query to find a keyword from one table to other table.

For Example,

I have two tables. 1st table contains the History records.

2nd table contains keyword.

If the keyword (Multiple) contains for example "Children" or "Family" or etc... in 1st table, then it should go and find all the rows in the 2nd table.

if keyword is matching in the 2nd table then it should list out all the Rows in the second table.

How to write the query...?

I want to display the result, when user logs in the application then based the user keyword it should find in the table and it must be display in the gridview...

How to write this...



1st Table (Key Word Table)

Keywords User
Children 101
Family 101
Room 102
Girl 102
Hostel 103
Shop 104

2nd Table (Searching Table)

Content
Small Boy is standing
There is a Girl
He is my Children
My Family is big
Family Room is available
Hostel is ready for bachelor
Shop is near by

Output

my paramenter is User 101. so the key word is "Children" and "Family".

so it must go and find the result in the second table.

Result should as below. it contains 3 rows

He is my Children

My Family is big
Family Room is available

推荐答案

您好,



试试这个......

Hi ,

Try This...
--1st Table (Key Word Table)
CREATE TABLE #KeyDtls(Keywords VARCHAR(50), UserID INT)
INSERT INTO #KeyDtls (Keywords , UserID )
SELECT 'Children', 101
UNION ALL SELECT 'Family', 101
UNION ALL SELECT 'Room', 102
UNION ALL SELECT 'Girl', 102
UNION ALL SELECT 'Hostel', 103
UNION ALL SELECT 'Shop', 104
 
--2nd Table (Searching Table)
CREATE TABLE #SeachTable(Content VARCHAR(100))
INSERT INTO #SeachTable (Content )
SELECT 'Small Boy is standing' 
UNION ALL SELECT 'There is a Girl' 
UNION ALL SELECT 'He is my Children' 
UNION ALL SELECT 'My Family is big' 
UNION ALL SELECT 'Family Room is available' 
UNION ALL SELECT 'Hostel is ready for bachelor' 
UNION ALL SELECT 'Shop is near by'

-- Source Data
SELECT * FROM #KeyDtls
SELECT * FROM #SeachTable 

-- Search Based on UserID
DECLARE @UserID INT=101

SELECT Content FROM #SeachTable 
INNER JOIN #KeyDtls T1 ON T1.UserID =@UserID
WHERE Content LIKE '%'+T1.Keywords+'%'





在代码项目帖子中搜索LIKE Search ....

问候,

GVPrabu



Search in Code Project Posts for LIKE Search....
Regards,
GVPrabu


这篇关于如何编写一个问题来在SQL中使用关键字从一个表到另一个表来查找结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆