Mysql 根据同一记录中的值选择字段

Mysql 根据同一记录中的值选择字段,mysql,sql,outer-join,Mysql,Sql,Outer Join,我想根据ID值选择一个字段 Products PRODUCT_ID Name 19 Chair 20 Table Product_fields ID PRODUCT_ID TYPE DESCRIPTION 1 19 C White 2 19 S Modern 3 20

我想根据
ID
值选择一个字段

Products
    PRODUCT_ID    Name
    19            Chair
    20            Table

Product_fields
    ID    PRODUCT_ID    TYPE    DESCRIPTION
    1     19            C       White
    2     19            S       Modern
    3     20            C       Black
    4     20            S       Classic
我需要这样的结果:

Product     Type_C   Type_S
Chair       White    Modern
Table       Black    Classic

我可以使用product_fields表上的两个
左联接来生成此结果,但这会大大降低查询速度。有更好的方法吗?

将查询速度减慢多少?什么是可以接受的

如果确实不想使用联接(必须有一个联接),则使用视图或嵌套查询。但是我不认为他们会更快,尽管你可以试试

请参阅


完整声明如下:

SELECT 
    NL.product_name,
    PRD.product_sku AS product_sku,
    CF.virtuemart_product_id AS virtuemart_product_id,
    GROUP_CONCAT(distinct CFA.customsforall_value_name
        ORDER BY CFA.customsforall_value_name ASC
        separator  ' | ' ) AS Name_exp_3,
    ROUND((((prices.product_price * CALC.calc_value) / 100) + prices.product_price),
            2) AS Prijs,
    VMCF_L.custom_value AS latijn,
    VMCF_T.custom_value AS THT
    VMCF_B.custom_value AS Batch
from j25_virtuemart_products AS PRD 
    LEFT join j25_virtuemart_product_custom_plg_customsforall AS CF     ON CF.virtuemart_product_id = PRD.virtuemart_product_id
    join j25_virtuemart_product_prices AS prices                ON PRD.virtuemart_product_id = prices.virtuemart_product_id
    join j25_virtuemart_calcs AS CALC                           ON prices.product_tax_id = CALC.virtuemart_calc_id
    join j25_virtuemart_products_nl_nl AS NL                    ON NL.virtuemart_product_id = PRD.virtuemart_product_id
    LEFT join j25_virtuemart_product_customfields AS VMCF           ON VMCF.virtuemart_product_id = PRD.virtuemart_product_id
    LEFT join j25_virtuemart_custom_plg_customsforall_values AS CFA     ON CFA.customsforall_value_id = CF.customsforall_value_id
    LEFT JOIN j25_virtuemart_product_customfields AS VMCF_L ON VMCF.virtuemart_product_id = VMCF_L.virtuemart_product_id AND VMCF_L.virtuemart_custom_id = 16
    LEFT JOIN j25_virtuemart_product_customfields AS VMCF_T ON VMCF.virtuemart_product_id = VMCF_T.virtuemart_product_id AND VMCF_T.virtuemart_custom_id = 3
    LEFT JOIN j25_virtuemart_product_customfields AS VMCF_B ON VMCF.virtuemart_product_id = VMCF_B.virtuemart_product_id AND VMCF_B.virtuemart_custom_id = 18
WHERE
    PRD.product_sku like '02.%'
group by PRD.virtuemart_product_id
order by NL.product_name;
其中,名为“Latijn”、“THT”和“Batch”的三个SELECT结果是我前面比较的黑/白和经典/现代值。 希望这有意义。 正如您所看到的,这涉及到一个Virtuemart安装,因此我无法对模式进行太多的修改

当我排除底部3个连接和相关字段时,查询大约需要0,5秒。包含连接和字段后,查询几乎需要19秒


我已经从这个完整的查询中创建了一个视图,我从我的标签应用程序中查询它。

谢谢大家!根据您的输入,我创建了:
选择
NL
product\u name
AS
product\u name
PRD
product\U sku
AS
product\U sku
CF
virtuemart\u product\u id
AS
virtuemart\u product\u id
,
集团公司(不同的
CFA
customsforall\u value\u name
CFA
订购
customsforall\u value\u name
ASC
分隔符“|”)为
Name_exp_3
,
四舍五入((((
prices
product\u price
*
CALC
CALC\u value
)/100)+
prices
product\u price
),
2) 作为Prijs
f
Latijn
AS
Latijn
f
THT
AS
THT
f
Batch
AS
Batch

从…起
()
左连接(
CF
virtuemart\u产品id
=
PRD
virtuemart\u产品id
))
加入((
PRD
virtuemart\u product\u id
=
prices
))上的
加入((
prices
product\u tax\u id
=
CALC
)上的
j25\u virtuemart\u calcs
CALC
)
加入(
nl
nl
nl
nl
virtuemart\u product\u id
=
PRD
virtuemart\u product\u id
))
左连接(
VMCF
VMCF
virtuemart\u product\u id
=
PRD
virtuemart\u product\u id
))
左连接(
CFA
customsforall\u id
=
CF
customsforall\u id
))
左连接(
f
virtuemart\u product\u id
=
PRD
virtuemart\u product\u id
))
哪里
(
PRD
产品_sku
如'02.%')
分组依据
PRD
virtuemart\u product\u id
NL
订购
产品名称``


这需要1.4秒来执行,比我开始时的19秒快了很多。

如果您想使用
LEFT JOIN
s,我可以假设您想要
产品中所有行的记录吗,我是否可以假设
product\u字段中的每个
product\u id
都会有一个类型为
C
和类型为
S
的记录?您能否向我们展示您的查询的最新版本以及表上的索引列表?如果设置了正确的键,连接应该会很快。
SELECT 
    NL.product_name,
    PRD.product_sku AS product_sku,
    CF.virtuemart_product_id AS virtuemart_product_id,
    GROUP_CONCAT(distinct CFA.customsforall_value_name
        ORDER BY CFA.customsforall_value_name ASC
        separator  ' | ' ) AS Name_exp_3,
    ROUND((((prices.product_price * CALC.calc_value) / 100) + prices.product_price),
            2) AS Prijs,
    VMCF_L.custom_value AS latijn,
    VMCF_T.custom_value AS THT
    VMCF_B.custom_value AS Batch
from j25_virtuemart_products AS PRD 
    LEFT join j25_virtuemart_product_custom_plg_customsforall AS CF     ON CF.virtuemart_product_id = PRD.virtuemart_product_id
    join j25_virtuemart_product_prices AS prices                ON PRD.virtuemart_product_id = prices.virtuemart_product_id
    join j25_virtuemart_calcs AS CALC                           ON prices.product_tax_id = CALC.virtuemart_calc_id
    join j25_virtuemart_products_nl_nl AS NL                    ON NL.virtuemart_product_id = PRD.virtuemart_product_id
    LEFT join j25_virtuemart_product_customfields AS VMCF           ON VMCF.virtuemart_product_id = PRD.virtuemart_product_id
    LEFT join j25_virtuemart_custom_plg_customsforall_values AS CFA     ON CFA.customsforall_value_id = CF.customsforall_value_id
    LEFT JOIN j25_virtuemart_product_customfields AS VMCF_L ON VMCF.virtuemart_product_id = VMCF_L.virtuemart_product_id AND VMCF_L.virtuemart_custom_id = 16
    LEFT JOIN j25_virtuemart_product_customfields AS VMCF_T ON VMCF.virtuemart_product_id = VMCF_T.virtuemart_product_id AND VMCF_T.virtuemart_custom_id = 3
    LEFT JOIN j25_virtuemart_product_customfields AS VMCF_B ON VMCF.virtuemart_product_id = VMCF_B.virtuemart_product_id AND VMCF_B.virtuemart_custom_id = 18
WHERE
    PRD.product_sku like '02.%'
group by PRD.virtuemart_product_id
order by NL.product_name;