Mysql,为多个值,不同标准选择相同的列

时间:2013-03-04 20:48:32

标签: mysql multi-select

我无法弄清楚如何使用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"

1 个答案:

答案 0 :(得分: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