简体   繁体   English

不同的mysql结果 - codeigniter

[英]Distinct mysql result - codeigniter

I`m using two tables to hold information about cars. 我用两张桌子来保存有关汽车的信息。 In the first table there is a information about car manufacturer etc and the second contain images for each car. 在第一个表格中有关于汽车制造商等的信息,第二个表格包含每辆汽车的图像。 My tables looks like this: 我的表看起来像这样:

car_information    car_images
---------------    ---------
id       | 2       car_id 2 | img1
car_make | Fiat    car_id 2 | img2
color    | Blue    car_id 2 | img3
etc ....

As you can see each car has three images. 你可以看到每辆车都有三张图片。

Here is query i`m using to fetch the result; 这是查询我用来获取结果;

 $this->db->select('
           c.id,
           c.car_make,
           c.car_color,
           ci.image
        ');       

  $this->db->from('car_information c');
  $this->db->join('car_images ci', 'ci.car_id = c.id', 'left');
  return $this->db->get();  

Everything works fine but the problems i`m facing is that results are duplicated. 一切正常,但我面临的问题是结果是重复的。

For example: 例如:

2 Fiat Blue img1
2 Fiat Blue img2
2 Fiat Blue img3

3 BMW white img4
3 BMW white img5

What to do so the result to looks like this 该怎么做的结果看起来像这样

2 Fiat Blue img1 | img 2 | img 3

I want to get all information in single row. 我想以单行获取所有信息。 I know that I can you two queries, but I`m wondering how to do with a single ? 我知道我可以向你提出两个问题,但我想知道如何处理一个问题?

Add the following line: $this->db->group_by('c.id'); 添加以下行: $this->db->group_by('c.id');

like so: 像这样:

 $this->db->select('
           c.id,
           c.car_make,
           c.car_color,
           ci.image
        ');       

  $this->db->from('car_information c');
  $this->db->join('car_images ci', 'ci.car_id = c.id', 'left');
  $this->db->group_by('c.id');
  return $this->db->get();  

This will work in MySQL because group by can be used as a synonym for distinct in MySQL. 这将在MySQL中起作用,因为group by可以用作MySQL中distinct的同义词。

Your joining 2 tables so you would get the same results from car_information if there are multiple images! 您加入2个表格,这样如果有多个图像,您将从car_information获得相同的结果! You have to do this as 2 queries or just use the first result array to extract the car info then loop over it and just use the image data. 您必须以2个查询的方式执行此操作,或者只使用第一个结果数组来提取汽车信息然后循环它并仅使用图像数据。

I think here's the answer you're looking for. 我想这就是你要找的答案。 http://codeigniter.com/forums/viewthread/100523/ Also read the codeigniter user_guide here http://codeigniter.com/user_guide/database/active_record.html http://codeigniter.com/forums/viewthread/100523/另请阅读codeigniter user_guide http://codeigniter.com/user_guide/database/active_record.html

Your query is correct but SQL doesn't work like that. 您的查询是正确的,但SQL不能像那样工作。 You get the raw data you asked, formatting must be done in your programming language (php in this case) as far as i know. 你得到你问的原始数据,格式化必须用你的编程语言(在这种情况下是php)完成,据我所知。

$this->db->select('
         c.id,
         c.car_make,
         c.car_color,
         ci.image
     ');       

$this->db->from('car_information c');
$this->db->join('car_images ci', 'ci.car_id = c.id', 'left');
$query = $this->db->get();  

$i = 1;
foreach ($query->result() as $row) {
 $car_array[$row->car_id][$i++] = $row->image;
}
var_dump($car_array);

that will create an array that will contain all images for a car_id 这将创建一个包含car_id所有图像的数组

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM