【问题标题】:Query giving double result instead of single查询给出双重结果而不是单一结果
【发布时间】:2019-05-17 15:51:41
【问题描述】:

我有两张桌子:products 和 current_product_attribute_values

我尝试了一个连接查询来根据user 选择的属性过滤它们,但是当我尝试使用附加条件时,它给了我 2 个结果而不是一个,它包括第一个不匹配的结果查询:

select * from `products` inner join `current_product_attribute_values` on `products`.`id` = `current_product_attribute_values`.`product_id` where `current_product_attribute_values`.`attribute_id` = ? or `current_product_attribute_values`.`attribute_value_id` = ? and `current_product_attribute_values`.`attribute_id` = ? or `current_product_attribute_values`.`attribute_value_id` = ? and `product_name` LIKE ?

这是我的 laravel 控制器代码:

 $all = Input::all();  

 $q = Input::get('search_text');

 $att_val = Input::get('attribute_value');

 $subcat = Input::get('subcat_id');

 $subcat_name = DB::table('subcategories')->where('id', $subcat)->value('subcategory_name');

 $brandname = DB::table('brands')->where('subcat_id', $subcat)->value('brand_name');

 $brand_id = DB::table('brands')->where('subcat_id', $subcat)->value('id');

 $product_count = DB::table('products')->where('brand_id', $brand_id)->count();

            if ($q != "") {

// getting multiple same name params    

                $query = DB::table('products');

                $query->join('current_product_attribute_values', 'products.id', '=', 'current_product_attribute_values.product_id');

                $j = 0;

                foreach ($all as $key => $values) {

                    //echo 'my current get key is :  ' . urldecode($key). '<br>';    

                    if ($key == $name[$j]) {

                        $query->where('current_product_attribute_values.attribute_id', '=', $att_id_value[$j]);
                        echo'<br>';
                        print_r($query->toSql());
                        echo'<br>';
                        //echo '<br> key matched and have some value : <br>';
                        //echo count($values);
                        if (count($values) >= 1) {

                             //echo '<br> it has array inside <br>';

                            foreach ($values as $val) {
                                //   or waali query in same attribute                                  
                                echo'<br>';
                                $query->orwhere('current_product_attribute_values.attribute_value_id', '=', $val);

                                print_r($query->toSql());
                                echo'<br>';
                            }
                        }

                        $j++;
                    }
                }

$records = $query->toSql();

                $query->where('product_name', 'LIKE', '%' . $q . '%');

                $records = $query->toSql();

print_r($records);

                $products = $query->paginate(10)->setPath('');

                $pagination = $products->appends(array(
                    'q' => Input::get('q')
                ));

                if (count($products) > 0) {

                    $filters = DB::table('product_attributes')->where('subcategory_id', $subcat)->get(['attribute_title']);
                } else {

                    $filters = array();
                }

                $categories = categories::where('add_to_menu', 1)->with('subcategories')->with('brands')->get();

                $categoryhome = categories::where('add_to_menu', 1)->with('subcategories')->get();

                return view('searchfilter')
                                ->with('productsdata', $products)
                                ->with('filtersdata', $filters)
                                ->with('categories', $categories)
                                ->with('categorieshome', $categoryhome)
                                ->with('subcat_name', $subcat_name)
                                ->with('subcat_id', $subcat)
                                ->with('brandname', $brandname)
                                ->with('product_count', $product_count)
                                ->with('querytext', $q);
            }
            return 'No Details found. Try to search again !';

【问题讨论】:

  • 样本数据和期望的结果真的很有帮助。
  • 感谢您的回复 Gordon Linoff 更新了我的问题.....
  • 你能提供一个期望结果的例子吗?

标签: sql laravel search filter


【解决方案1】:

如果您使用原始 sql 作为调用 db select 函数,它会更容易。例如:

$query=DB::select("select * from `products` inner join `current_product_attribute_values` on `products`.`id` = `current_product_attribute_values`.`product_id` where `current_product_attribute_values`.`attribute_id` = ? or `current_product_attribute_values`.`attribute_value_id` = ? and `current_product_attribute_values`.`attribute_id` = ? or `current_product_attribute_values`.`attribute_value_id` = ? and `product_name` LIKE ?
");

确实,如果需要,您可以在原始 sql 中连接变量,例如:

$queryBrands = "select id from brands where subcat_id =".$subcat;
//echo $queryBrands
$queryBrands = DB::select($queryBrands);

【讨论】:

  • 感谢您的回复 ruff0 我也使用了它,但问题是我的和条件无法正常工作,因为它正在给我所有的结果,比如或没有检查这两个条件
【解决方案2】:

通过查看您的表格,id 值 17 的 product 表格在 current_product_attribute_values 列 product_id 中有 两条 记录(我假设这个列用作product 表的外键)。

使用select *,您可以从两个表中选择所有列。所以它很可能会导致您的查询返回多条记录。

我的建议:

  • 只选择您需要的列。从长远来看,避免使用select *,即select product.id, product.description, current_product_attribute_values.attribute_values ......
  • 使用GROUP BY

希望这些有所帮助。

【讨论】:

  • 感谢您的回复 cipley,但这里这些相似的记录具有不同的属性,所以当我过滤它们时,例如 RAM 64GB 和处理器 Demo3,那么我只想要每个产品的单一结果,但在我的情况下,它给了我多个不同的记录,例如给出的声明
  • 问题是你使用了select *,所以不同值的记录也会显示在结果集中。这就是为什么您应该只选择要显示的列,并根据需要使用GROUP BY 或DISTINCT。
猜你喜欢
  • 1970-01-01
  • 2019-03-06
  • 2021-12-22
  • 1970-01-01
  • 1970-01-01
  • 2014-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多