在sql中搜索3个连接表

时间:2018-01-26 09:34:42

标签: php sql wordpress

我目前在WordPress中遇到sql查询问题。所以我遇到的问题是由于某种原因我似乎无法在连接表中搜索而不会得到表不存在错误。

我的查询如下:

$userOrders = $wpdb->prefix.'user_orders';
$users = $wpdb->prefix.'users';
$userAddedInfo = $wpdb->prefix.'user_added_info';
$userData = $postData;
$query = "select u.*, ue.*, pmo.*
  from $users u
  LEFT JOIN
  $userAddedInfo ue on ue.userId = u.id
  right JOIN
  $userOrders pmo on pmo.userId = u.id";
$whereClausePrefix = ' where';

基于条件的if语句如下:

  if (!empty($userData->locationSearchText)) {
     $conditions = $whereClausePrefix." $userAddedInfo.city = '".$userData->locationSearchText."'";
 if ($whereClausePrefix === ' where') {
    $whereClausePrefix === ' and';
  }
}

最后我的执行声明如下:

if (!empty($conditions)) {
 $results =  $query .$conditions;
}
if (empty($conditions)) {
 $results =  $query;
}
$results = $wpdb->get_results($results);
return $results;

我的错误信息如下:

<div id="error"><p class="wpdberror"><strong>WordPress database error:</strong> [Column &#039;city&#039; in where clause is ambiguous]<br /><code>select u.*, ue.*, pmo.*
  from wp_dmp_lucystevens_users u
  LEFT JOIN
  wp_dmp_lucystevens_user_added_info ue on ue.userId = u.id
  right JOIN
  wp_dmp_lucystevens_user_orders pmo on pmo.userId = u.id where city = &#039;Cape Town&#039;</code></p></div>{"data":[]}

0 个答案:

没有答案