简体   繁体   English

在包含两个表的记录的交叉表的单个查询中选择三个记录-MYSQL

[英]Select three records in a single query of a cross table that contains records of two tables - MYSQL

I have a main table called "product" that is linked with three tables: 我有一个称为"product"的主表,该主表与三个表链接:

"product_type", 
"feature", 
"type_feature" 

and a cross table called "product_feature" that contains several features of the same product. 还有一个称为"product_feature"的交叉表,其中包含同一产品的多个功能。

Example one record: 示例一记录:

I have something similar like this: 我有类似的东西:

product_type 产品类别

id_product_type    name
    1              Phone

feature 特征

id_feature   name
    1         Memory 
    2         Color 
    3         Memory Ram

feature_type FEATURE_TYPE

id_feature_type  id_feature  value
      1              1         16GB
      2              1         32GB
      3              2         Blue
      4              2         Black
      5              3         2GB
      6              3         3GB  

product 产品

id_product id_product_type  price quantity  model
    1           1           100$    5      Moto-G7 

Cross table "product_feature" (linked to "product", "feature" and "feature_type"): 交叉表“ product_feature”(链接到“ product”,“ feature”和“ feature_type”):

id_feature id_product id_feature_type
    1          1            1
    2          1            3
    3          1            6

I want query show this: 我想查询显示此:

id_product   type_name price quantity   model    feature_name  value  
    1        Phone     100$     5      Moto-G7     Memory       16GB
feature_name2 value2  feature_name3   value3
    Color      Blue    Memory Ram       3GB

I tried this, but only I have only one feature_name and value, and I need three: 我试过了,但是只有我只有一个feature_name和value,我需要三个:

  SELECT p.id_product, pt.name, model, price, quantity, f.name AS feature, ft.value
  FROM product p
  LEFT JOIN product_type pt ON pt.id_product_type = p.id_product_type 
  LEFT JOIN product_feature pf ON pf.id_product = p.id_product
  LEFT JOIN feature f ON f.id_feature = pf.id_feature
  LEFT JOIN feature_type ft ON ft.id_feature_type = pf.id_feature_type
  GROUP BY p.id_product;

Try this query, It will give you the results: 试试这个查询,它将为您提供结果:

SELECT product.*, product_type.name as TypeName, (SELECT CONCAT( GROUP_CONCAT(feature_type.value), "|", GROUP_CONCAT(feature.name) ) FROM `product_feature` INNER JOIN feature_type on product_feature.id_feature_type = feature_type.id_feature_type INNER JOIN feature on feature_type.id_feature = feature.id_feature WHERE product_feature.id_product=1 GROUP BY product_feature.id_product) as options FROM `product` LEFT JOIN product_type on product.id_product_type = product_type.id_product_type

在此处输入图片说明

You will get options for your product and you have to explode() those options by "|" 您将获得产品的选项,并且必须通过“ |”将这些选项炸开()。 and you can count how many options you have by using $count= count(explode("|", $options)) 并且您可以通过使用$ count = count(explode(“ |”,$ options))来计数您拥有多少个选项

By this way you can get all options . 通过这种方式,您可以获得所有选择。

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

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