如果db结果有重复且重复的结果具有值特定值,则如何隐藏db结果

时间:2017-12-14 09:14:42

标签: php sql wordpress

我正在尝试从数据库获取所有产品,但隐藏这些重复且具有meta_value'CX'的产品。一个产品可以包含meta_value ='NB','CX'或'RN'。 如果meta_value是CX-hide,如果它的'NB'或'RN' - 显示它们。但是如果产品是重复的并且一个的meta_value是NB而另一个是CX - 不显示。如果NB和RN - 显示。 我的代码:

    global $wpdb;
    $results = $wpdb->get_results($wpdb->prepare("SELECT id FROM wp_frm_items WHERE user_id=%d ORDER BY created_at DESC", $user_id));
    //$array = array();

    if ($results) {
    foreach($results as $row) {

      $policy_transaction_type = $wpdb->get_results($wpdb->prepare("SELECT meta_value FROM wp_frm_item_metas WHERE field_id=%d AND item_id=%s", 390, $row->id));
      $policy_transaction_type = $policy_transaction_type[0]->meta_value;
      $policy_id = $wpdb->get_results($wpdb->prepare("SELECT  meta_value FROM wp_frm_item_metas WHERE field_id=%d AND item_id=%s", 203, $row->id));
      $policy_start_date = $wpdb->get_results($wpdb->prepare("SELECT  meta_value FROM wp_frm_item_metas WHERE field_id=%d AND item_id=%s", 170, $row->id));
      $policy_type_data = $wpdb->get_results($wpdb->prepare("SELECT  meta_value FROM wp_frm_item_metas WHERE field_id=%d AND item_id=%s", 200, $row->id));
      $policy_type = $policy_type_data[0]->meta_value;
      // $policy_id = $policy_id[0]->meta_value;
      $policy_wording = ($policy_type == 'Static Caravan / Park Home / Chalet')?'/wp-content/uploads/2017/08/Static-Caravan-Wording.pdf':($policy_type == 'Touring Caravan'?'/wp-content/uploads/2017/08/Tourer-Wording.pdf':'/wp-content/uploads/2017/08/Lodge-and-Park-Home-Wording.pdf');
      $policy_summary = ($policy_type == 'Static Caravan / Park Home / Chalet')?'/wp-content/uploads/2017/08/Static-Caravan-Policy-Summary.pdf':($policy_type == 'Touring Caravan'?'/wp-content/uploads/2017/08/Tourer-Policy-Summary.pdf':'/wp-content/uploads/2017/08/Lodge-and-Park-Home-Policy-Summary.pdf');
      $dateUK = date("d/m/Y", strtotime($policy_start_date[0]->meta_value));
      if(!empty($policy_id)){
        $array[] = $policy_id[0]->meta_value;
      }     

       //foreach ($array as $key) {
       if ($policy_transaction_type != 'CX') {

但现在返回所有产品。 提前致谢

1 个答案:

答案 0 :(得分:0)

您的代码似乎没有正确的方法。我建议改用排除逻辑。以下是如何做到这一点:

$excludes=array();
$results_exclude = $wpdb->get_results(
 "SELECT item_id FROM wp_frm_item_metas where meta_value LIKE '%CX%'"
);

foreach($results_exclude as $rex) {$excludes[]=$rex->item_id;}

$results = $wpdb->get_results(
$wpdb->prepare(
"SELECT id FROM wp_frm_items WHERE user_id=%d and id NOT IN (%s) 
 ORDER BY created_at DESC",
$user_id,implode(',',$excludes)));

这就是全部。 $ results将为您提供没有meta_value = CX行的ID。