If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > General > Database Concepts & Design > How to join search result in one query on mutil table

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-04-10, 13:10
DBConfuser DBConfuser is offline
Registered User
 
Join Date: Nov 2010
Posts: 3
How to join search result in one query on mutil table

Hi everyone.

I have a not difficult issue, however, I do not know how to implement it.

my case is:

I have two table. table_A, table_B,
table_a {id, name}
table_b {id, address}
what i am want to do is:

i want to find some text in both table with in column name or address.
maybe you will ask me why not create one table, sorry, there is some reason there.

let me continue my case, for example, I want to search record with text: mike.

select A.name from table_A where name like "text%"
select B.address from table_B where address like "text%"

is there a way to query both table at same time.

like:
select A.name, B.address from table_A, table_b where A.name like "text%" or B.address like "text%" and A.id != B.id;

thanks.
Reply With Quote
  #2 (permalink)  
Old 11-04-10, 13:23
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by DBConfuser View Post
is there a way to query both table at same time.
yes, with a union query
Code:
SELECT A.name AS found_column
     , 'A'    AS found_table
  FROM table_A AS A
 WHERE name LIKE 'text%'
UNION ALL
SELECT B.address 
     , 'B'
  FROM table_B AS B
 WHERE address LIKE 'text%'
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 11-04-10, 13:35
DBConfuser DBConfuser is offline
Registered User
 
Join Date: Nov 2010
Posts: 3
Talking

that was awesome,

and it works great.

Thanks R937.

if i want to remove the same ID record. how to do it?

sorry I am new pie for database and so greedy.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On