Click here to Skip to main content
15,920,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I would like to know on how to search for a data in different table but with only one query if it is possible, example;

SQL
select * from table1,table2,table3 WHERE id="$searctext";


is this query applicable or there's any better query?

for your information, those 4 tables have all the same attributes, for example, table1 has id name address.

so does the table 2,3,4 but did not relate to one another, so im going to search for example by id, I input it as 001, and what the query do is it search through all 4 tables to obtain the id and all information about the id.

Please help me with this, all help are much appreciated :)
Posted
Updated 4-Oct-11 16:09pm
v3

You can use the following query provided the columns match exactly :
SQL
select * from table1 where id = 'somevalue' 
union 
select * from table2 where id = 'somevalue'
union
...
 
Share this answer
 
I agree with Mehdi's answer, but I'll add this in case you need to know which table each row is coming from:

SELECT *, 'table1' AS tbl FROM table1 WHERE id='$searchtext'
UNION
SELECT *, 'table2' AS tbl FROM table2 WHERE id='$searchtext'
UNION
...
 
Share this answer
 
I assume you will have the same id value in multi0ple tables ? and have to use UNION ALL

The UNION operator selects only distinct values by default. To allow duplicate values, use UNION ALL.

SQL
SELECT *  FROM table1 WHERE id='$searchtext'
UNION ALL
SELECT *  FROM table2 WHERE id='$searchtext'
UNION ALL
...
 
Share this answer
 
v3
Thank you guys,all your solution helped me a lot,you guys indeed an expert :)
 
Share this answer
 

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