I’m trying to write a query that looks through four tables and pulls in all the relevant data. My get_phocadownload_user_stat table tracks every download ever made on my site. I need all of these to show up regardless of whether the data for each download in subsequent tables is missing or not. The problem is the query is just grabbing the data for which there is a result in every data piece I query. IE if there isn’t a result for user_ip.ip_address for a user who downloaded something the download information doesn’t show at all. Ideally I’d like at the end of the query to do something like:
if($result['ip_address'] == '' || $result['ip_address'] == NULL){
print "N/A";
}
Here is my query string:
$string = "SELECT get_phocadownload_user_stat.fileid, get_phocadownload_user_stat.count, get_phocadownload_user_stat.date, get_users.name, get_users.username, get_users.email, get_phocadownload.filename, user_ip.ip_address, user_ip.location
FROM get_phocadownload_user_stat
INNER JOIN get_users ON get_phocadownload_user_stat.userid = get_users.id
INNER JOIN get_phocadownload ON get_phocadownload_user_stat.fileid = get_phocadownload.id
INNER JOIN user_ip ON get_users.username = user_ip.username
ORDER BY get_phocadownload_user_stat.date DESC";
Is there a simple way to tell the query that if a result isn’t available just default to NULL instead of skipping over it?
Try using a
LEFT JOINinstead of anINNER JOIN, as ‘the LEFT JOIN keyword returns all rows from the left table (table_name1), even if there are no matches in the right table (table_name2).’