【发布时间】:2017-02-19 19:55:59
【问题描述】:
我有两个输入字段。两者都导致将在数据库上匹配的颜色数组。 数组可以是这样的
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);
}
这会返回错误
“where 子句中的列‘颜色’不明确”
“从image左连接image_topcolor打开image_topcolor.image_id=image.id左连接image_bottomcolor左连接image_bottomcolor@.image@.= 987654334 @左加入color AS a ON image_topcolor 987654338 @ a 987654340 color AS b 987654343 image_bottomcolor 987654345 b 987654345 b 987654345 b 987654346@ 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 |
抱歉,帖子太长了。
问题回顾:
使用两个输入时,如何获得具有匹配颜色的所有图像的结果?就像我只使用一个输入时得到的一样。
谢谢!
更新
此查询现在不会产生任何错误。
但是,如果用户选择不存在的颜色,我仍然没有得到结果。如果图像在两个表中具有相同的选定颜色,我只会得到结果。
示例:图像具有红色、蓝色顶部颜色和蓝色、绿色底部颜色。我无法通过搜索红色顶部和绿色底部来获得此结果。但是,如果我搜索蓝色顶部和蓝色底部,我会得到结果。我猜这与连接有关?
$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);
}
【问题讨论】:
标签: php mysql codeigniter search join