Solved

how to do this in query

Posted on 2013-01-28
11
286 Views
Last Modified: 2013-02-11
I have a query like below

query 1

select name from table1 where id=....

will return a list of name


query 2

select * from table2 where table2.id like '%query1%'

this doesn't work. How to do query 2 to return the records that match any query 1 returned???
0
Comment
Question by:wasabi3689
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 38829140
>How to do query 2 to return the records that match any query 1 returned???

SELECT name
FROM Query2
WHERE id IN (SELECT id FROM Query1)

btw the attached image will help
SQL Joins
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 38829161
But a quicker way (generally) would be:

SELECT Query2.name
FROM Query2
INNER JOIN (SELECT id FROM Query1) as Temp
ON Query2.ID = Temp.ID
0
 

Author Comment

by:wasabi3689
ID: 38829228
this is not exact what I want. Let me clear out here


There is no relationship between query 1 and query 2. But in query 2, the field called "name" contains word from the name returned from query 1.

I want to return any records whose name field contain the word from query 1

query 1

select name from table1 where id=....

will return a list of name


query 2

select * from table2 where table2.name like '%query1%
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 47

Accepted Solution

by:
Dale Fye (Access MVP) earned 400 total points
ID: 38829264
How about:

SELECT *
FROM Table2, Query1
WHERE Table2.[Name] Like '%' & query1.FieldName & '%'
0
 

Author Comment

by:wasabi3689
ID: 38829282
hi fyed,

I have this returned

The data types varchar and varchar are incompatible in the '&' operator.
0
 

Author Comment

by:wasabi3689
ID: 38829293
query1.FieldName is varchar(64) data type

Table2.[Name] is text data type
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 38829344
My experience with this aspect of SQL Server is limited. You might be able to use either the CAST( ) or CONVERT( ) SQL functions, something like:

SELECT *
FROM Table2, Query1
WHERE Cast(Table2.[Name] as varchar(64)) Like '%' + query1.FieldName + '%'
0
 

Author Comment

by:wasabi3689
ID: 38829385
the same error?

Can I convert this as procedure? I mean, have a while loop then, return each record matched. If so, how?
0
 
LVL 10

Expert Comment

by:deviprasadg
ID: 38830466
Please check this:
SELECT distinct T2.* FROM table1 T1 CROSS JOIN table2 T2
WHERE T2.name LIKE ('%'+T1.name+'%')

Open in new window


Also you can add the condition on id
SELECT distinct T2.* FROM table1 T1 CROSS JOIN table2 T2
WHERE T2.name LIKE ('%'+T1.name+'%')
and t1.id=..

Open in new window

0
 
LVL 32

Expert Comment

by:awking00
ID: 38831610
select * from table2 t2
where exists
(select 1 from table1 t1
 where charindex(t1.name,t2.name) > 0);
0
 
LVL 32

Expert Comment

by:awking00
ID: 38831620
Might need AS keyword for table aliases -
select * from table2 AS t2
where exists
(select 1 from table1 AS t1
 where charindex(t1.name,t2.name) > 0);
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
This query failed in sql 2014 5 33
MS SQL BCP Extra Lines Between Records 2 24
Job - date manual 1 35
SSRS: Why is Visual Studio stripping these properties? 2 23
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question