[英]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.