2012-02-23 6 views
0

codeigniter를 사용하고 있으며, 3 개의 다른 테이블을 호출하여 하나의 배열로 결합하려고합니다.MySQL이 3 개의 테이블과 형식을 결합합니다.

$this->db->select('so_alcohol.id, 
    so_alcohol.name as name, 
    so_alcohol.category as category, 
    so_alcohol.permName as permName, 
    so_alcohol.picture as alcoholPicture, 
    group_concat(so_user.firstName) as firstName, 
    group_concat(so_user.lastName) as lastName, 
    group_concat(so_user.picture) as userPicture, 
    group_concat(so_rating.idUser) as idUser, 
    group_concat(so_rating.overall) as overall, 
    group_concat(so_rating.description) as description'); 
$this->db->from('alcohol'); 
$this->db->order_by("id", "desc"); 
$this->db->join('so_rating', 'so_rating.idAlcohol = so_alcohol.id', 'LEFT'); 
$this->db->join('so_user', 'so_user.id = so_rating.idUser', 'LEFT'); 
$query = $this->db->get(); 

싶습니다 출력은 다음과 같다 : 이것은 내가 무엇을 가지고

Array 
(
    [0] => stdClass Object 
      (
        [id] => 1 
        [name] => Product Name 
        [category] => Product category 
        [permName] => Product-Name 
        [alcoholPicture] => http://image.com/1.jpg 
        [idUser] => 1,2,3 
        [overall] => 100,80,50 
        [description] => Review 1,Awesome review 2, Horrible review 3 
        [firstName] => name 1, name 2, name 3 
        [lastName] => last 1, last 2, last 3 
        [userPicture] => http://userimage.com/1.jpg, http://userimage.com/2.jpg, http://userimage.com/3.jpg 
       ) 
    [0] => stdClass Object 
      (
        [id] => 1 
        [name] => Product Name 
        [category] => Product category 
        [permName] => Product-Name 
        [alcoholPicture] => http://image.com/1.jpg 
        [idUser] => 1,2,3 
        [overall] => 100,80,50 
        [description] => Review 1,Awesome review 2, Horrible review 3 
        [firstName] => name 1, name 2, name 3 
        [lastName] => last 1, last 2, last 3 
        [userPicture] => http://userimage.com/1.jpg, http://userimage.com/2.jpg, http://userimage.com/3.jpg 
       ) 

) 

내 질문에 내가 지금이 문제 (즉 I가 나는 일이 무엇을해야합니까 어떻게 찾았습니다.) 먼저 1 개의 결과 만 출력합니다. 40을 출력하고 두 번째를 나타냅니다. so_user.id = so_rating.idUser 왜냐하면 이미 group_concat(so_rating.idUser)의 아이디어가 있습니까? 난 아직도 PHP 초보자 로서이 일을하는 올바른 방법인지 확실하지 않습니다.

편집 :

알코올

id  name   category  permName  picture 
1   Product #1  1    Product-1  http://someurl.com/1.jpg 
2   Product #2  1    Product-2  http://someurl.com/2.jpg 
3   Product #3  1    Product-2  http://someurl.com/3.jpg 
4   Product #4  2    Product-2  http://someurl.com/4.jpg 

평가

id  idUser  idProduct  overall  description 
1   1    2    100   Great Product Review 1 
2   2    2    75    Great Product Review 2  
3   1    3    100   Great Product Review 3  
4   2    3    98    Great Product Review 4 

사용자

id  firstName  lastName  userPicture 
1   first 1   last 1   http://someurlUserPicture.com/1.jpg    
2   first 2   last 2   http://someurlUserPicture.com/2.jpg     
3   first 3   last 3   http://someurlUserPicture.com/3.jpg     
4   first 4   last 4   http://someurlUserPicture.com/4.jpg 

답변

2

함께 작업하는 몇 가지 테이블 설계도를 제공 할 수 있습니까? 귀하의 검색어에 GROUP BY도 표시되지 않습니다. 너 GROUP BY so_alcohol.id을 원한다고 생각하니? so_alcohol.id으로 그룹화하려는 경우 max(so_alcohol.name) as name과 같은 나머지 열에 대해서는 집계 함수를 사용해야합니다. further reading about max()

두 번째 문제는 실제로 없습니다. 결과에 포함시키지 않더라도 사용 가능한 모든 열에 참여할 수 있습니다. 예 :

SELECT 
    MAX(customer.email) AS email, 
    MAX(customer.name) AS name, 
    GROUP_CONCAT(address.country) AS countries 
FROM 
    customer 
JOIN 
    /* customer.address_id is not in the SELECT-clause, 
     but you can still use the column to join */ 
    address ON customer.address_id = address.id 
GROUP BY 
    customer.id 

나중에 firstName의 값을 단일 이름으로 분리하려고 생각하면하지 마십시오. mysql이 행당 한 명의 사용자를 생성하고 출력하는 동안 PHP에서 그룹화를 수행하십시오.

EDIT1 :

TABLE 제품

id name 
1 Product1 
2 Product2 

TABLE 평가

rating_id product_id 
1   1 
2   1 
3   2 

BY GROUP에 예

SELECT * 
FROM product 
JOIN rating ON product.id = rating.product_id 

출력

가입 17,451,515,
id name  rating_id product_id 
1 Product1 1   1 
1 Product1 2   1 
2 Product2 3   2 

추가, GROUP BY id

-clause
SELECT 
    id, 
    MAX(name) AS name, 
    COUNT(rating_id) AS rating_count 
FROM product 
JOIN rating ON product.id = rating.product_id 
GROUP BY id 

하나 개의 그룹으로 열 id에 같은 값이있는 모든 튜플을 넣어 것입니다 :

id name  rating_count 
1 Product1 2 
2 Product2 1 

당신이 열을 선택 할 수 없습니다 결과를 그룹화하거나 집계 함수를 사용하지 마십시오. 그러나 mysql은 이것을 미끄러지게하고 어쨌든 합리적인 결과를 제공합니다.

미니 튜토리얼이 작동되기를 바랍니다. 그렇지 않으면 나는 codeigniter를 계속 사용하기 전에 SQL을 더 잘 알기를 제안한다. 커튼 뒤에서 코드 기가하는 것을 이해하지 못하면 출력물을 제어 할 수 없습니다.

+0

나는 도표의 도표를 작성했다. 당신의 요구에 따라 기능별 그룹이 무엇을 할 지 확신하지 못했습니다. php/msql을 사용하여 여전히 녹색입니다. 희망을 통해 내가 배워야 할 것을 배울 수있는 사이트를 만드는 것이 었습니다. 두 번째 부분은 mysql에서'so_user.id = so_rating.idUser'를 수행 할 때 오류가 발생한다는 것입니다. 이전에'GROUP_CONCAT'했기 때문입니다. 테이블 구조도를 올리 자마자 이걸 어떻게 처리 할 것인지 예를 보여 주시겠습니까? – Jacinto

+0

오류 메시지를 복사 해 주시겠습니까? – Basti

+0

신경 쓰지 말라 나는 바보이다, 나는 끝에 넣지 않았다. 좋아, 두 번째 부분은 작동하지만 여전히 1 개의 알코올, 1 개의 리뷰 및 1 명의 사용자 만 얻고 있습니다. 40 개의 알코올, 5 리뷰, 5 명의 사용자를 얻고 싶습니다. – Jacinto

관련 문제