【问题标题】:Codeigniter - Join same table and search with arrayCodeigniter - 加入同一张表并使用数组搜索
【发布时间】: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


    【解决方案1】:

    您没有正确使用 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); 
    }
    

    【讨论】:

    • 我仍然收到“where 子句中的列'颜色'不明确”错误,我将在帖子中编辑更多信息
    • 能不能有别的地方你有where条件?错误消息清楚地说明了 where 条件。能否请您粘贴您生成的 sql 语句?
    • 我已经添加了生成的sql
    • 在我看来,您的代码中还有一些其他条件。你确定你删除了你第一次尝试的代码形式吗? if(!empty($bottom_colors[0])) { $this->db->where_in('color', $bottom_colors); } if(!empty($top_colors[0])) { $this->db->where_in('color', $top_colors); }
    • 感谢您的帮助!我在帖子底部添加了更新
    【解决方案2】:

    由于您两次连接同一个表,这两个表具有完全相同的字段集。因此,每次引用这两个表之一的字段时,都需要在字段名称前加上表别名,例如a.color

    我相信codeigniter可以通过以下方式实现:

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

    【讨论】:

    • 好的!更近了一步。不过,我仍然收到“where 子句中的“列'颜色'不明确”错误。我的选择看起来像这样 "$this->db->select('*'); " 我也应该在选择中写下特定的颜色别名吗?
    • 使用 select * 不需要为各个列指定表别名,尽管在客户端区分没有字段别名的 2 个表的字段可能有点困难。您还有其他使用颜色字段的 where 标准吗?你能把生成的sql粘贴到上面的问题中吗?
    • 我已经添加了生成的sql
    • 正如 moni_dragu 所指出的:您必须留下一些其他代码干扰您当前的代码。您可能必须在查询构建器上使用fluch_cache() 或reset() 方法来从您执行的先前查询中删除任何条件。
    • sql 现在可以正常工作而没有给我错误。但我仍然没有得到我想要的结果。正如我在仅使用一个输入时提到的那样,即使用户选择了不匹配的颜色,我也会得到图像。这似乎不适用于两个输入。我在底部添加了更新。非常感谢您的帮助(:
    猜你喜欢
    • 2015-07-10
    • 1970-01-01
    • 1970-01-01
    • 2019-11-05
    • 2011-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多