1

我无法弄清楚如何使用 where 子句创建多个选择,以便我的名称字段中的三个场景中的每一个都有一个列。

我现在的查询是:

    SELECT
    pd_extrafields.packetDetailsId,
    pd_extrafields.ex_value as Credit 
    FROM
    pd_extrafields
    where pd_extrafields.ex_title = "Credit,A,B,C,D,E,F"

如何在以下条件下将其他两列拉入同一个查询(基本上我想将三个查询合并为一个并保留列名):

    SELECT
    pd_extrafields.packetDetailsId,
    pd_extrafields.ex_value as AccountNum 
    FROM
    pd_extrafields
    where pd_extrafields.ex_title = "Account#"

&

    SELECT
    pd_extrafields.packetDetailsId,
    pd_extrafields.ex_value as OrderStat 
    FROM
    pd_extrafields
    where pd_extrafields.ex_title = "New/Existing"
4

1 回答 1

1

使用CASE.

 select packetDetailsId, GROUP_CONCAT(Credit) AS Credit, GROUP_CONCAT(AccountNum) AS AccountNum, GROUP_CONCAT(OrderStat) AS OrderStat FROM
( SELECT pd_extrafields.packetDetailsId,
    CASE WHEN pd_extrafields.ex_title LIKE ('%Credit%') 
    THEN pd_extrafields.ex_value ELSE NULL END as Credit,
    CASE WHEN pd_extrafields.ex_title LIKE ('%Account%') 
    THEN pd_extrafields.ex_value ELSE NULL END as AccountNum,
    CASE WHEN pd_extrafields.ex_title LIKE ('%Existing%') 
    THEN pd_extrafields.ex_value ELSE NULL END as OrderStat
    FROM pd_extrafields) AS TempTab GROUP BY packetDetailsId
于 2013-03-04T20:54:51.953 回答