Q:

Doing a join on 3 tables

I have 3 sql tables 
1) a table with headers of a coupon - id of this equals the id of the second table 
2) a tables with details of the coupon - user_id on this tables equals user id of the third table
3) a table with details of user  
So far I have this query 
"SELECT kpn_processed_deals.kpn_id,
kpn_processed_deals.purchased_date, kpn_processed_deals.claim, 
kpn_processed_deals.uid,kpn_deal_headers.kpn_type,
kpn_deal_headers.title,kpn_deal_headers.created_by
 FROM kpn_processed_deals INNER JOIN kpn_deal_headers ON
 kpn_processed_deals.kpn_id = kpn_deal_headers.kpn_id AND 
kpn_deal_headers.created_by = '$var'";

This works just fine but I want to get the value of the users email on the third table using a join but I have been unsuccessful so far. Sorry if my formatting is messy. I'm horrible at these things.

A:

Just add another JOIN.
"SELECT p.kpn_id, p.purchased_date, p.claim, p.uid,h.kpn_type, h.title, h.created_by, u.email
 FROM kpn_processed_deals AS p INNER
 JOIN kpn_deal_headers AS h ON p.kpn_id = h.kpn_id
 JOIN kpn_deal_users AS u ON u.user_id = p.user_id
 WHERE h.created_by = '$var'";

Also notice the use of table aliases, so you don't have to repeat the verbose table names throughout the query.
And constraints on single tables should normally be in the WHERE clause; the ON clause is for conditions related to joining the tables (an exception is in outer joins, where constraints on the child table need to be in the ON clause as well).

