php - Codeigniter - 加入同一个表并用数组搜索

标签 php mysql codeigniter search join

我有两个输入字段。两者都导致将在数据库中匹配的颜色数组。 数组看起来像这样

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

一个输入用于搜索名为“image_topcolor”的表,另一个输入用于搜索另一个表“image_bottomcolor”。
我正在尝试提出一种结构来根据颜色选择搜索具有这两个输入的图像。

我希望用户能够选择多种颜色,然后返回数据库中具有所选颜色的所有图像。 使用以下代码仅使用一个输入字段时,搜索工作正常:

$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); 
}

即使用户选择了没有与图像匹配的 id 的颜色,这也会将图像返回给用户,这太完美了!

我希望这在使用两个输入字段查找与输入颜色匹配的所有图像时以相同的方式工作。

我想使用类似的东西:

$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); 
}

返回错误

"Column 'color' in where clause is ambiguous"

“SELECT * FROM image LEFT JOIN image_topcolor ON image_topcolorimage_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')"

我不确定如何使用这两个数组来查找与任何选定颜色匹配的所有图像。
我的表格如下所示:

图片

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

颜色

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

image_topcolor

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

image_bottomcolor

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

抱歉发了这么长的帖子。 问题回顾: 使用两个输入时,如何获得具有匹配颜色的所有图像的结果?就像我只使用一个输入时得到的一样。
谢谢!

更新
此查询现在未生成任何错误。
但是如果用户选择了一种不存在的颜色,我仍然得不到结果。如果图像在两个表中具有相同的选定颜色,我只会得到结果。 示例:图像具有红色、蓝色 top_color 和蓝色、绿色底部颜色。我无法通过搜索红色顶部和绿色底部来获得此结果。但是,如果我搜索 blue top 和 blue bottom,我会得到结果。我想这与连接有关?

$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);

}

最佳答案

您没有正确使用 where_in 语法。您需要使用字段名称作为函数的第一个参数,而不是表名称(别名)。

试试这个:

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); 
}

更新: 为了获得您想要的结果,您还应该使用 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); 
}

关于php - Codeigniter - 加入同一个表并用数组搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39974461/

相关文章:

mysql - 这样安全吗?这在 MYSQL 中可以吗?

php - 根据归一化保存文章及相关分类

php - 如何反转字符串并替换字符?

php - Magento : How to change the markup of category page?

php - Laravel Schedule withoutOverlapping() 不适用于 runInBackground()

php - jQuery 和 php 图像旋转拼图

mysql - 如何创建MySQL分层递归查询

php - 何时使用存储过程和触发器与应用层

php - Codeigniter 电子邮件 - `554 SMTP 同步错误

jquery - Blueimp jQuery 文件上传插件与 Codeigniter 一步一步?