Click here to Skip to main content
15,901,426 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Comments
[no name] 4-Mar-13 9:14am    
I think you need to look up how to use "LIKE"

1 solution

Hi ,

Try This...
SQL
--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+'%'


Search in Code Project Posts for LIKE Search....
Regards,
GVPrabu
 
Share this answer
 
Comments
gani7787 5-Mar-13 0:22am    
Thanks for your great help..

The result is coming....But, it is taking too much time to get the results...for example it is taking 1 Minutes 05 Seconds.

Can we modify the query to reduce the time to get the immediate results....?

pls.help....
.
gvprabu 5-Mar-13 0:23am    
how many rows are there in your tables?
gani7787 20-Mar-13 10:18am    
10000 rows....also searching every columns contains bulk of employee data's.

we will have alternate option. such that FullTextSearch...

But, this is disable. i need a simple query to getting the n number of results...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900