2

以下のようなさまざまなショップへのユーザー注文用のテーブル構造があります。今、クエリを取得したいのですが、codeigniter になります

  1. user_id に属する shop_id の選択リスト
  2. 製品のリストを選択し、shop_id と user_id に属します
  3. 次に、1 つの配列になります。

このように:これについてのクエリをください。別の関数を試しましたが、結合を使用する単一の関数が必要です
user_id -> shop_id -> products [0] ->product_name1 [1] -> product_name2`

Table : at_order
+----+------------+-----------+------------+
| id | user_id    | shop_id   | product-id |
+----+------------+-----------+------------+
|  1 | 3          | 3         |          32|
|  2 | 3          | 3         |          24|
|  3 | 3          | 4         |          3 |
|  4 | 4          | 3         |          8 |
|  5 | 4          | 5         |          4 |
|  6 | 4          | 6         |          1 |
+----+------------+-----------+------------+

function main () 
{
    SELECT
      `shop_id`
    FROM (`at_order`)
    WHERE `user_id` = '3'
    GROUP BY `shop_id`;
    one($shop_id,$user_id);

}

function one () 
{
    SELECT
      `product_id`,
      `order_status`
    FROM (`at_order`)
    WHERE `user_id` = '3'
        AND `shop_id` = '5';
    two($shop_id,$product_id);
}

function two () 
{
    SELECT *
    FROM (`at_product`)
    WHERE `product_id` = '35'
        AND `shop_id` = '5'
}

これらの 3 つのクエリを 3 つの関数で記述し、この関数をメイン関数に結合しました。

Please help
4

1 に答える 1

2

ユーザーIDのみを必要とする単一のクエリを使用して、必要なものをすべて取得できます。

function getUserProducts($user_id){

    $query  =   "SELECT
                    as.shop_name,
                    ap.*
                FROM at_order ao
                LEFT JOIN at_product ap 
                    ON ap.product_id = ao.product_id
                LEFT JOIN at_shops as
                    ON as.shop_id = ao.shop_id
                WHERE ao.user_id = $user_id";
    return $this->db->query($query)->result_array();

    //Or Active Record Version

    return $this->db
                ->select('ao.shop_name')
                ->select('ap.*')
                ->from('at_order ao')
                ->join('at_product ap ','ap.product_id = ao.product_id','left')
                ->join('at_shops as','as.shop_id = ao.shop_id','left')
                ->where('ao.user_id',$user_id)
                ->get()
                ->result_array();
}
于 2013-10-26T06:37:45.940 に答える