If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Joining two tables?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-15-03, 08:55
Cybercitizen Cybercitizen is offline
Registered User
 
Join Date: Dec 2003
Posts: 2
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.
Reply With Quote
  #2 (permalink)  
Old 12-15-03, 09:11
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,533
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/
Reply With Quote
  #3 (permalink)  
Old 12-15-03, 15:36
Cybercitizen Cybercitizen is offline
Registered User
 
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.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On