1

たとえば、いくつかのテーブルがあります:
製品:

| product_id | name   | price |
| 1          | apple  | 20.32 |
| 2          | pear   | 9.99  |
| 3          | banana | 1.5   |

製品属性:

| attr_id | name   | value |
| 1       | weight | 10 kg |
| 2       | date   | 2013  |
| 3       | color  | red   |

...等々。
最後に製品属性関係表:

| product_id | attr_id |
| 1          | 3       |
| 2          | 1       |
| 1          | 2       |
| 3          | 2       |

私の質問: 次のデータ構造 (または同様のもの) で製品 1 と 2 を返す 1 つの選択要求クエリを構築することはできますか? ここで、最初に "where product_id IN (1, 2)" という選択要求を実行してから、それらの属性をループ選択する必要があります。
下手な英語でごめんなさい:]

array(
    [0] = array(
          product_id = 1,
          name = apple,
          attributes= array(
                        [0] => array(
                           attr_id = 3,
                           name = color,
                           value = red,
                        ),
                        [0] => array(
                           attr_id = 2,
                           name = date,
                           value = 2013,
                        ) 

                      ),
    ),
    [1] = array(
          product_id = 2,
          name = apple,
          attributes= array(
                        [0] => array(
                           attr_id = 1,
                           name = veight,
                           value = 10 kg,
                        ),
                      ),
    )  
)
4

2 に答える 2

1

これはクエリだけの問題ではなく、PHP コードの問題でもあります。これは適合します:

$rSelect = mysqli_query('SELECT
     products.id AS record_id,
     products.name AS products_name,
     products.price AS product_price, 
     attributes.id AS attribute_id,
     attributes.name AS attribute_name,
     attributes.value AS attribute_value
   FROM
     products 
     LEFT JOIN products_attributes
       ON products.id=products_attributes.product_id
     LEFT JOIN attributes
       ON products_attributes.attr_id=attributes.id', $rConnect);

$rgResult = [];
while($rgRow = mysqli_fetch_array($rSelect))
{
   $rgResult[$rgRow['record_id']]['product_id']   = $rgRow['record_id'];
   $rgResult[$rgRow['record_id']]['name']         = $rgRow['product_name'];
   $rgResult[$rgRow['record_id']]['price']        = $rgRow['product_price'];
   $rgResult[$rgRow['record_id']]['attributes'][] = [
      'attr_id' => $rgRow['attribute_id'],
      'name'    => $rgRow['attribute_name'],
      'value'   => $rgRow['attribute_value'],
   ];
};
//var_dump($rgResult);
于 2013-08-26T12:16:14.510 に答える
0

探しているクエリは次のとおりです。

select
    p.product_id,
    p.name as product_name,
    a.attr_id,
    a.name as attr_name,
    a.value
from
    products as p
    inner join `product-attribute` as pa on p.id = pa.product_id
    inner join attribute as a on pa.attr_id = a.attr_id

これにより、多次元配列を作成できる場所から単純な結果テーブルが返されます。

このクエリで次の 2 つの点に気付く場合があります。

  • 名前に予約されているダッシュが含まれているため、`テーブル名の前後にバッククォートを使用します。そのため、名前をバッククォートに入れてエスケープする必要があります。product-attribute-
  • フィールド名nameがあいまいであるため、エイリアス ( product_name/ attr_name) を付けて、後でエイリアス名で参照できるようにします。
于 2013-08-26T12:22:55.330 に答える