LEFT JOIN, INNER OUTER JOIN, LEFT OUTER JOIN is driving me crazy. Please help?

Hi @Holidaymaine:

Not sure where you are doing wrong, but try the following instead which is a self contained test.php file you can drop into the root of your website and load in your browser with http://yoursite.com/test.php (assuming you replace yoursite.com with your site’s domain! Also, note how I adding in a WHERE {$wpdb->usermeta}.meta_key = 'last_name' to ensure you did not get records you were not expecting):

<?php
/*
Filename: test.php
*/

include('wp-load.php');

$sql =<<<SQL
SELECT
  {$wpdb->users}.user_email,
  {$wpdb->usermeta}.meta_value
FROM
  {$wpdb->users}
  LEFT JOIN {$wpdb->usermeta} ON {$wpdb->users}.ID = {$wpdb->usermeta}.user_id
WHERE 1=1
  AND {$wpdb->users}.user_status="0"
  AND {$wpdb->usermeta}.meta_key = 'last_name'
  AND {$wpdb->usermeta}.meta_value="Smith"
SQL;
  $usersemails = $wpdb->get_results($sql);
  header('Content-type:text/plain');
  print_r($usersemails);

Leave a Comment