tags:

views:

111

answers:

3

I'm trying to create a directory and I have two tables, entry and location. Each entry will be able to have multiple locations so I'm assuming the best idea (correct if wrong) is to create a third table where I specify 'links'. Example:

Marly:France
Karla:Argentina
Smith:USA
Katie:France
Smith:United Kingdom
Katie:USA

When I want to list people from France I'd go into that third table and pull up names where location = France. I'm fine with doing everything up to this point, but now I need to retrieve data from the entry table for each Name of each person in France (according to the third table). I'm pretty sure I've seen this done before. Any pointers? :D

+1  A: 
SELECT `entry`.*
FROM `entry`
JOIN `entry-locations` ON `entry-locations`.`entry-id` = `entry`.`name`
WHERE `entry-locations`.`location` = 'Aberdeen'

$res = mysql_query('all-that-stuff-above');
while($row = mysql_fetch_assoc($res)) {
    echo $row['name'] . "\n";
}
mysql_free_result($res);
chaos
I'd limit the columns returned on that puppy, and I'd add a few NOT IS NULL clauses in the WHERE section. Otherwise, you'll be getting some unintended results.
Eric
I only know for sure that OP wants the entry name, so I limited to that. As to the rest, you mean IS NOT NULL, and they aren't necessary; NULL will never match 'France'.
chaos
Isn't INNER JOIN better in this case? If we want, e.g., to list people from France, we don't want to see ppl w/o countries.
Kuroki Kaze
I actually need all the information from the entry table for each name located in 'France'.
zuk1
@Kuroki Kaze: Yeah. I doubt it actually makes a difference here, but it's better practice. Edited. @zuk1: Okay, edited accordingly.
chaos
So after I have performed such a query I just take the field data as I normally would?
zuk1
I don't know how you normally would, but most likely, yes. :)
chaos
I just can't get this to work at all :S The query I have is:SELECT `entry`.*FROM `entry`JOIN `entry-locations` ON `entry-locations`.`entry-id` = `entry`.`name`JOIN `locations` ON `entry-locations`.`location` = `locations`.`name`WHERE `locations`.`name` = 'Aberdeen'And it doesn't bring up any errors so it may be correct. Could you show me some working PHP code to extract field data from the entry table (with location Aberdeen) using this query?
zuk1
@zuk1: Okay, done. I also simplified the query based on the fact that you're apparently using the entries' and locations' names as primary keys.
chaos
+1  A: 
SELECT
    entryt.name AS name,
    locationt.location AS location
FROM locationt, entryt
WHERE locationt.location = "France"

Remember: Change the names I used there:

  • I named the entry table as "entryt"
  • And the location table as "locationt"

Should return "name" with all the people that live in France.

Pretty sure that would work. Still, correct me if I'm wrong.

Daniel S
A: 

SELECT * FROM entry WHERE name in (SELECT name FROM links WHERE location = 'FRANCE');

I have assumed the table names as 'entry' & 'links' and column names as 'entry.name' and 'links.location'.

StackKrish