1 SQL from more than on table without foreign key

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

1 SQL from more than on table without foreign key

Post by anjanesh »

I have 3 tables - table1, table2, table3 - all have a field called Name
table1 has 20 Names
table1 has 30 Names
table1 has 50 Names

I want all the names from all the tables in one query (100 names) :
SELECT * FROM table1,table2,table3 will return 30000 names (repeats)
and SELECT DISTINCT(t1.Name),DISTINCT(t2.Name),DISTINCT(t3.Name) FROM table1 t1, table2 t2, table3 t3 doesnt work.

What other options are there ?
Thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you have to attach the tables together somehow in order to use joins correctly or you will get multiples.

maybe you should look into a [mysql_man]union[/mysql_man].. and post your queries in

Code: Select all

tags when [u]not[/u] in php code, please....
Post Reply