2012-02-23 13 views
0

私はcodeigniterを使用しています。私は3つの異なるテーブルを呼び出して1つの配列に結合しようとしています。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を出力する必要があります.2番目の私はso_user.id = so_rating.idUserのため、私はすでにgroup_concat(so_rating.idUser)のアイデアを持っていませんか?私はまだPHPの初心者であるようにこれが正しい方法であるかどうかはわかりません。

EDIT:

アルコール

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()

あなたの2番目の問題は本当にありません。あなたが結果にそれを持っていない場合でも、利用可能な列に参加することができます。例:

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に行ごとに1人のユーザーを生成させ、出力中にPHPでグループ化します。

EDIT1:

TABLE製品

id name 
1 Product1 
2 Product2 

TABLE評価

rating_id product_id 
1   1 
2   1 
3   2 

参加

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

出力

BY GROUPの例
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 

1つのグループに列idに同じ値を持つすべてのタプルを置く:

id name  rating_count 
1 Product1 2 
2 Product2 1 

あなたが列を選択することができません集計関数を使用してグループ化したり、集計関数を使用したりすることはできません。しかし、mysqlはこれをスリップさせ、とにかく妥当な結果を提供します。

ミニチュートリアルがうまくいくことを願っています。そうでない場合は、codeigniterを使い続ける前にSQLをよく知ることをお勧めします。あなたがカーテンの背後で何をしているのかを理解できなければ、出力を制御することはできません。

+0

私は表の回路図を載せています。機能別にどのグループが何をしているのか分かりません....私はまだphp/msqlで本当に緑色です。私が望むべきことを学ぶためのサイトを作ることが私の希望です。 2番目の部分は、私が以前に 'GROUP_CONCAT'していたので、私は' so_user.id = so_rating.idUser'をやっていないとMysqlがいつもエラーを出しています。あなたは私がテーブルスケマティックを立てた今、これにどのように取り組むかの例を私に見せてもらえますか? – Jacinto

+0

エラーメッセージをコピーできますか? – Basti

+0

私はばかです、私は最後には入れませんでした。 2番目の部分はうまくいきますが、まだ1つのアルコール、1つのレビュー、1人のユーザーしかいません。私は40のアルコール、5人のレビューと5人のユーザーを取得したい – Jacinto

関連する問題