带有OR

时间:2016-08-01 09:48:22

标签: mysql codeigniter

我有这个查询,当我添加OR时,返回多行,我该如何解决这个问题?我需要另一个左连接或右连接?

SELECT `images`.`id` as `id_image`, `images_lang`.`title` as `title`, `images_lang`.`autor` as `author`, `media_lang`.`file`, `category_lang`.`title` as `category` 
FROM `images` 
JOIN `images_lang` ON `images_lang`.`id_images` = `images`.`id` 
JOIN `category_lang` ON `images`.`id_category` = `category_lang`.`id_category` 
JOIN `media` ON `images`.`id` = `media`.`id_item` 
JOIN `media_lang` ON `media`.`id` = `media_lang`.`id_media` 
JOIN `relation` ON `relation`.`from_id` = `images`.`id` 
JOIN `tag` ON `tag`.`id` = `relation`.`to_id` 
JOIN `tag_lang` ON `tag`.`id` = `tag_lang`.`id_lang` 
WHERE `media`.`table` = 'images' 
AND `media_lang`.`id_lang` = '1' 
AND `images_lang`.`id_lang` = '1' 
AND `category_lang`.`id_lang` = '1' 
AND `images`.`active` = 1 
AND `relation`.`type` = 2 
AND `tag_lang`.`id_lang` = '1' 
AND `tag_lang`.`slug` = 'pa-am-oli' 
OR `tag_lang`.`slug` = 'playa'
LIMIT 9

和codeigniter中的代码,但我不知道如何添加或之后的($ keywords是一个用于过滤元素的数组)

 $this->db->select('images.id as id_image, images_lang.title as title, images_lang.autor as author, media_lang.file, category_lang.title as category');
        $this->db->from($this->table);
        $this->db->join($this->table.'_lang',$this->table.'_lang.id_images = '.$this->table.'.id');
        $this->db->join('category_lang',$this->table.'.id_category = category_lang.id_category');
        $this->db->join('media',$this->table.'.id = media.id_item');
        $this->db->join('media_lang','media.id = media_lang.id_media');
        $this->db->where('media.table',$this->table);
        $this->db->where('media_lang.id_lang',$id_lang);
        $this->db->where($this->table.'_lang.id_lang', $id_lang);
        $this->db->where('category_lang.id_lang', $id_lang);
        $this->db->where('images.active', 1);

        if($category){
            $this->db->where('category_lang.title', $category);
        }

        if($keywords){
            $this->db->join('relation', 'relation.from_id = '.$this->table.'.id');
            $this->db->join('tag', 'tag.id = relation.to_id');
            $this->db->join('tag_lang', 'tag.id = tag_lang.id_lang');
            $this->db->where('relation.type', _IMAGES_2_TAGS_);
            $this->db->where('tag_lang.id_lang', $id_lang);
            foreach($keywords as $tag){

                $this->db->or_where('tag_lang.slug', $tag);

            }

        }

        if($from || $limit)
        {
            $this->db->limit((int)$limit, (int)$from);
        }

        $query = $this->db->get();

        return $query->result_array();

3 个答案:

答案 0 :(得分:0)

我没有完全得到你需要的东西,但如果你想通过使用OR过滤只有一行作为回报

在您的情况下,您应该执行以下操作:

WHERE (`media`.`table` = 'images' OR `tag_lang`.`slug` = 'playa')
AND (`media_lang`.`id_lang` = '1' OR `tag_lang`.`slug` = 'playa')
等等,我希望能帮到你,祝你好运!

答案 1 :(得分:0)

我解决了添加此问题的查询(以及选择区别)

AND (`tag_lang`.`slug` = 'playa'
OR `tag_lang`.`slug` = 'pa-am-oli')

我如何在codeigniter中添加它?

答案 2 :(得分:0)

明确使用括号可以解决您的问题,但这更容易阅读......

SELECT i.id id_image
     , il.title 
     , il.autor author
     , ml.file
     , cl.title category 
  FROM images i
  JOIN images_lang il
    ON il.id_images = i.id 
  JOIN category_lang cl
    ON cl.id_category = i.id_category
  JOIN media m
    ON m.id_item = i.id
  JOIN media_lang ml
    ON ml.id_media = m.id
  JOIN relation r
    ON r.from_id = i.id 
  JOIN tag t
    ON t.id = r.to_id 
  JOIN tag_lang tl
    ON tl.id_lang = t.id 
 WHERE m.table = 'images' 
   AND ml.id_lang = 1 
   AND il.id_lang = 1 
   AND cl.id_lang = 1 
   AND i.active = 1 
   AND r.type = 2 
   AND tl.id_lang = 1 
   AND tl.slug IN('pa-am-oli','playa')
 LIMIT 9

...但请注意,没有ORDER BY的LIMIT是毫无意义的。