我有两个输入字段。两者都会导致将在数据库上匹配的颜色数组。数组可能如下所示
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);
}
这将返回错误
"子句含糊不清的'颜色'列">
"从左image
选择*加入image_topcolor
image_topcolor
。image_id
= image
.id
左加入image_bottomcolor
image_bottomcolor
。image_id
= image
.id
左加盟color
作为a
image_topcolor
。color_id
= a
.id
左加盟color
作为b
image_bottomcolor
。color_id
= b
.id
color
("蓝色"(和color
("红色"(和a
的位置。color
输入("红色"(和b
。color
IN('蓝色'(">
我不确定如何使用这两个数组来查找与任何选定颜色匹配的所有图像。
我的表如下所示:
图像
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和蓝色、绿色底部颜色。我无法通过搜索红色顶部和绿色底部来获得此结果。但是,如果我搜索蓝色顶部和蓝色底部,我会得到结果。我想这与连接有关?
$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);
}
由于您联接了同一个表两次,因此这两个表具有完全相同的字段集。因此,每次引用这两个表之一中的字段时,都需要在字段名称前面加上表别名,例如 a.color
。
我相信在编码点火器中,您可以通过以下方式实现这一目标:
...
$this->db->where_in('a.color', $top_colors);
$this->db->where_in('b.color', $bottom_colors);
...