Forum Moderators: coopster

Message Too Old, No Replies

How to pull from multiple database tables

...without multiple queries

         

twist

3:49 am on Jan 12, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In table one is a list of names,

name_1,
name_2,
name_3,
...

In the second table their are photos related to each name. Some names have multiple photos, some names have none. The correct photo for each name will be marked as "selected". So for example,

SELECT photo_name FROM $table2 WHERE name = 'name_1' AND photo = 'selected'

What I want to do is create a page listing 50 names and each name will have the selected picture next to it if it exists, but the only way I know how to approach this would be to run a query on table 1 once and 50 queries on table 2.


$result = mysql_query( "SELECT name FROM $table1" );
while( $row = mysql_fetch_array( $result ) ) {

$query = "SELECT photo_name FROM $table2 WHERE name = 'name_1' AND photo = 'selected'";
...

}

This seems highly inneficient and I know their has to be a better way to approach this. Any help or links to a good tutorial would be much appreciated.

FalseDawn

7:02 am on Jan 12, 2006 (gmt 0)

10+ Year Member



Do a search for "SQL Table Joins" or variations thereof.