简体   繁体   English

Codeigniter - 加入相同的表并使用数组进行搜索

[英]Codeigniter - Join same table and search with array

I have two input fields. 我有两个输入字段。 Both leading to an array array of colors that will be matched on the database. 两者都导致将在数据库上匹配的颜色数组。 The arrays can look like this 数组看起来像这样

Array ( [0] => red [1] => blue [2] => green ) 

One input is used for searching a table called ´image_topcolor´ and the other input is searching another table, ´image_bottomcolor´. 一个输入用于搜索名为'image_topcolor'的表,另一个输入用于搜索另一个表'image_bottomcolor'。
I'm trying to come up with a structure to search for images with these two inputs based on color selection. 我正在尝试提出一种结构,根据颜色选择使用这两个输入来搜索图像。

I want the user to be able to select several colors and then return all images which has the selected color in the database. 我希望用户能够选择多种颜色,然后返回数据库中具有所选颜色的所有图像。 The search works fine when using only one input field with the following code: 仅使用一个带有以下代码的输入字段时,搜索工作正常:

$this->db->select('*'); 
$this->db->from('image');

if(!empty($top_colors[0]) && empty($bottom_colors[0])) {
$this->db->join('image_topcolor', 'image_topcolor.image_id = image.id' ,'inner');
$this->db->join('color', 'color.id = image_topcolor.color_id', 'inner');
}
if(!empty($bottom_colors[0]) && empty($top_colors[0])) {    
$this->db->join('image_bottomcolor', 'image_bottomcolor.image_id = image.id' ,'inner'); 
$this->db->join('color', 'color.id = image_bottomcolor.color_id', 'inner');
}

if(!empty($bottom_colors[0])) {     
$this->db->where_in('color', $bottom_colors); 
}
if(!empty($top_colors[0])) {
$this->db->where_in('color', $top_colors); 
}

This returns images to the user even if the user has selected a color that doesn't have an id matched with an image yet, which is perfect! 即使用户选择的颜色没有与图像匹配的ID,这也会将图像返回给用户,这是完美的!

I want this to work the same way when using both input fields to find all images that matches the colors from the inputs. 当使用两个输入字段查找与输入颜色匹配的所有图像时,我希望此方法的工作方式相同。

I want to use something similiar to: 我想使用类似的东西:

$this->db->select('*'); 
$this->db->from('image');

if(!empty($top_colors[0]) && !empty($bottom_colors[0])) {
$this->db->join('image_topcolor', 'image_topcolor.image_id = image.id' ,'left');
$this->db->join('image_bottomcolor', 'image_bottomcolor.image_id = image.id' ,'left');      
$this->db->join('color as a', 'image_topcolor.color_id = a.id','left');
$this->db->join('color as b', 'image_bottomcolor.color_id = b.id','left');
}

if(!empty($top_colors[0]) && !empty($bottom_colors[0])) {
$this->db->where_in('a', $top_colors); 
$this->db->where_in('b', $bottom_colors); 
}

This returns with the error 这将返回错误

"Column 'color' in where clause is ambiguous" “where子句中的列'颜色'含糊不清”

"SELECT * FROM image LEFT JOIN image_topcolor ON image_topcolor . image_id = image . id LEFT JOIN image_bottomcolor ON image_bottomcolor . image_id = image . id LEFT JOIN color as a ON image_topcolor . color_id = a . id LEFT JOIN color as b ON image_bottomcolor . color_id = b . id WHERE color IN('blue') AND color IN('red') AND a . color IN('red') AND b . color IN('blue')" “SELECT * FROM image LEFT JOIN image_topcolor ON image_topcolorimage_id = imageid LEFT JOIN image_bottomcolor ON image_bottomcolorimage_id = imageid LEFT JOIN color作为a ON image_topcolorcolor_id = aid LEFT JOIN colorb ON image_bottomcolorcolor_id = bid WHERE color IN( '蓝色')和color IN( '红')和acolor IN( '红')和bcolor IN( '蓝')”

I'm not sure how I can use both arrays to find all images that matches any of the selected colors. 我不确定如何使用这两个数组来查找匹配任何所选颜色的所有图像。
My tables looks like this: 我的表看起来像这样:

image 图片

id | color | info
---|-------|------
1  | blue  | foo
2  | red   | bar

color 颜色

id | color |
---|-------|
1  | red   |
2  | blue  | 
3  | green |

image_topcolor image_topcolor

image_id | color_id |
---------|----------|
1        | 1        |
1        | 2        |

image_bottomcolor image_bottomcolor

image_id | color_id |
---------|----------|
2        | 1        |
2        | 3        |

Sorry for the long post. 对不起,很长的帖子。 Question recap: How can I get the results of all images with matching colors when using both inputs? 问题回顾:在使用两个输入时,如何获得具有匹配颜色的所有图像的结果? Just like I get when using only one input. 就像我只使用一个输入时得到的一样。
Thanks! 谢谢!

Update 更新
This query is now not generating any errors. 此查询现在不会生成任何错误。
But I'm still not getting the results if the user selects a color that doesn't exist. 但是如果用户选择不存在的颜色,我仍然没有得到结果。 I only get the result if the image has the same selected color in both tables. 如果图像在两个表中具有相同的选定颜色,我只得到结果。 Example: An image has red, blue top_color and blue, green bottom color. 示例:图像具有红色,蓝色top_color和蓝色,绿色底部颜色。 I cannot get this result by searching for red top and green bottom. 通过搜索红顶和绿底,我无法得到这个结果。 However if I search for blue top and blue bottom, I get the result. 但是,如果我搜索蓝色顶部和蓝色底部,我会得到结果。 I guess this has do with the joins? 我想这与连接有关吗?

$this->db->select('*'); 
    $this->db->from('image');
    if(!empty($top_colors[0]) && !empty($bottom_colors[0])) {
    $this->db->join('image_topcolor', 'image_topcolor.image_id = image.id' ,'left');
    $this->db->join('image_bottomcolor', 'image_bottomcolor.image_id = image.id' ,'left');  


$this->db->join('color as a', 'image_topcolor.color_id = a.id','left');
$this->db->join('color as b', 'image_bottomcolor.color_id = b.id','left');

if(!empty($top_colors[0]) && !empty($bottom_colors[0])) {
$result = array_merge($top_colors, $bottom_colors);

$this->db->where_in('a.color', $top_colors); 
$this->db->where_in('b.color', $bottom_colors);

}

Since you joined the same table twice, the two tables have exactly the same set of fields. 由于您连续两次加入同一个表,因此这两个表具有完全相同的字段集。 Therefore, every time you reference a field from one of these two tables, you need to prefix the field name with the table alias, such as a.color . 因此,每次从这两个表中的一个引用字段时,都需要在字段名称前加上表别名,例如a.color

I believe in codeigniter you can achieve this in the following way: 我相信codeigniter你可以通过以下方式实现这一点:

...
$this->db->where_in('a.color', $top_colors); 
$this->db->where_in('b.color', $bottom_colors);
...

You did not use the where_in syntax correct. 您没有使用where_in语法正确。 You need to use the name of the field as the first argument of the function, not the table name (alias). 您需要使用字段的名称作为函数的第一个参数,而不是表名(别名)。

Try this: 尝试这个:

if(!empty($top_colors[0]) && !empty($bottom_colors[0])) {
$this->db->where_in('a.color', $top_colors); 
$this->db->where_in('b.color', $bottom_colors); 
}

UPDATE: To get your desired result you should also use or_where_in, like this: 更新:要获得所需的结果,您还应该使用or_where_in,如下所示:

if(!empty($top_colors[0]) && !empty($bottom_colors[0])) {
  $this->db->where_in('a.color', $top_colors);
  $this->db->or_where_in('b.color', $bottom_colors); 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM