[英]Codeigniter - Join same table and search with array
我有两个输入字段。 两者都导致将在数据库上匹配的颜色数组。 数组看起来像这样
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子句中的列'颜色'含糊不清”
“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
作为a
ON image_topcolor
。 color_id
= a
。 id
LEFT JOIN color
为b
ON image_bottomcolor
。 color_id
= b
。 id
WHERE color
IN( '蓝色')和color
IN( '红')和a
。 color
IN( '红')和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);
}
由于您连续两次加入同一个表,因此这两个表具有完全相同的字段集。 因此,每次从这两个表中的一个引用字段时,都需要在字段名称前加上表别名,例如a.color
。
我相信codeigniter你可以通过以下方式实现这一点:
...
$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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.