Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2003
    Posts
    2

    Unanswered: Joining two tables?

    Hi,

    I just started with PHP and mySQL, and as a newbie I'm having some trouble with joining two tables.

    In the beginning I just needed all subscribers to a newsletter, and that was easy:

    Code:
    $query = "SELECT email_address FROM members
    WHERE receive_newsletter=1";
    $result = mysql_query($query);
    while(list($email_address) = mysql_fetch_row($result))
    {
    print("$email_address&#59; ");
    }
    However, I would like to link the e-mail address to the country of origin as well, for instance Belgium and France. The problem (for me) is the "country" resides in a different table called "address_book".

    If I do something like this (see below), my browser stops responding:

    Code:
    $query = "SELECT t1.*, t2.* FROM address_book AS t1, members AS t2
    WHERE t1.country_id=be OR t1.country_id=fr AND t2.receive_newsletter=1";
    $result = mysql_query($query);
    
    while(list($email_address) = mysql_fetch_row($result))
    {
    print("$email_address&#59; ");
    }
    Any help appreciated. I have a feeling this can be done easily, it's just that I lack the knowledge.

    Probably worth mentioning is that both tables have a column called "member_id".

    Thanks in advance.

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Code:
    select t1.*
         , t2.*
      from address_book as t1
    inner
      join members as t2
        on t1.member_id = t2.member_id
    where t1.country_id in ('be','fr')
      and t2.receive_newsletter=1
    rudy
    http:?/r937.com/

  3. #3
    Join Date
    Dec 2003
    Posts
    2
    Thanks. I will try it as soon as the server is back online. Time to dive into those tutorials.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •