繁体   English   中英

MySQL在一个查询中多项选择

[英]MySQL multiple selects in one query

例如,我有几个表:
产品介绍:

| 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? 现在,我应该先运行deveral选择请求“ 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,
                        ),
                      ),
    )  
)

这不仅与查询有关,还与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);

您要查找的查询是:

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

这将返回一个简单的结果表,您可以在其中创建多维数组。

您可能会在此查询中注意到两件事:

  • 我在表名称product-attribute周围使用反引号` ,因为它在名称中包含一个破折号-这是保留名称。 因此,您需要将其放在反引号中以避开该名称。
  • 因为字段名称name不明确,所以我给它们起了一个别名( product_name / attr_name ),以便以后仍可以用别名来引用它们。

暂无
暂无

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

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