从同一个表到具有相同标识符的单行的多行

时间:2015-03-24 04:26:07

标签: mysql left-join multiple-columns

我是SQL查询的新手,我正在尝试使用类似标识符将多个行压缩为单行。以下是一个例子:

Table orders:

groupid | question   | answer

|-----------------------------------|

abc000  | First Name | John 

abc000  | Last Name  | Doe

abc000  | City       | Denver

...     | ...        | ...

abc000  | Entry      | Individual

abc000  | Shirt Size | X-Large

abc001  | First Name | Jane

abc001  | Last Name  | Doe

abc001  | City       | Seattle

...     | ...        | ...

abc001  | Entry      | Individual

(No Shirt entry)

我试图让它看起来像:

表格确认(所需输出):

groupid |姓氏|名字|城市| ...... |衬衫尺寸

| ---------------------------------------------- ------------ |

abc000 | Doe |约翰|丹佛| ...... | X大

abc001 | Doe |简|西雅图| ...... |小

表格确认(实际输出):

groupid |姓氏|名字|城市| ...... |衬衫尺寸

| ---------------------------------------------- ------------ |

abc000 | Doe |约翰|丹佛| ...... |

abc000 | Doe |约翰|丹佛| ...... | X大

abc001 | Doe |简|西雅图| ...... |

查询是:

SELECT DISTINCT om.groupid, om.fid, om.eid, f.subtitle,
        IF(oi.question = 'First Name', oi.answer, '') AS fname,
        IF(oi1.question = 'Last Name', oi1.answer, '') AS lname,
        IF(oi2.question = 'City', oi2.answer, '') AS city,
        IF(oi3.question = 'State', oi3.answer, '') AS state,
        IF(oi4.question = 'Birthdate', oi4.answer, '') AS bday,
        IF(oi5.question = 'Gender', oi5.answer, '') AS gender,
        IF ( (oi6.question LIKE '%fee'), oi6.answer, '') as event,
        IF ( (oi7.question = 'shirt size (unisex cotton)'), oi7.answer, '') as shirt
    FROM ordermeta om
    JOIN orders o ON o.orderid=om.orderid
    LEFT JOIN orderitems oi ON oi.groupid=om.groupid
    LEFT JOIN orderitems oi1 ON oi1.groupid=om.groupid
    LEFT JOIN orderitems oi2 ON oi2.groupid=om.groupid
    LEFT JOIN orderitems oi3 ON oi3.groupid=om.groupid
    LEFT JOIN orderitems oi4 ON oi4.groupid=om.groupid
    LEFT JOIN orderitems oi5 ON oi5.groupid=om.groupid
    LEFT JOIN orderitems oi6 ON oi6.groupid=om.groupid
    LEFT JOIN orderitems oi7 ON oi7.groupid=om.groupid
    WHERE  oi.question = 'First NAME'
        AND oi1.question = 'Last Name'
        AND oi2.question = 'City'
        AND oi3.question = 'State'
        AND oi4.question = 'Birthdate'
        AND oi5.question = 'Gender'
        AND oi6.question LIKE '%fee%'
    ORDER BY lname ASC

如果我删除衬衫尺寸,它的效果非常好。我添加了:

AND oi7.question = "Shirt Size" 

除了它可能不总是“衬衫尺寸”,但更多LIKE“%衬衫尺寸%”,当没有问题=衬衫尺寸时,我希望它留下空或空白。使用AND,它只会给我带有衬衫问题的结果。 (140个结果中的55个)

当前代码,当question =“shirt size”存在时,我得到两个条目(一个是空白,另一个是好信息)。所有其他条目都有一个没有信息的条目。 (140个结果中的195个)

我尝试过多种不同的东西,比如CASE,IF THEN语句。在我添加衬衫问题之前,他们所有人都做同样的事情。

如果您需要更多信息,请告诉我们。感谢。

我尝试过的其中一种SE类型的示例: Combine multiple rows into one row MySQL

2 个答案:

答案 0 :(得分:0)

我发现有两个问题"衬衫"。这是创建第二个条目,适合模型,留下第二行空白,因为它不适合选择的第二个过滤器。这是我最终要编写的内容。

SELECT DISTINCT om.groupid, om.fid, om.eid, f.subtitle,
        IF(oi.question = 'First Name', oi.answer, '') AS fname,
        IF(oi1.question = 'Last Name', oi1.answer, '') AS lname,
        IF(oi2.question = 'City', oi2.answer, '') AS city,
        IF(oi3.question = 'State', oi3.answer, '') AS state,
        IF(oi4.question = 'Birthdate', oi4.answer, '') AS bday,
        IF(oi5.question = 'Gender', oi5.answer, '') AS gender,
        IF ( (oi6.question LIKE '%fee'), oi6.answer, '') as event,
        IF ( (oi7.question IN ('Youth Shirts','Event Shirt','Tech Shirt Size','Series Shirt','Choose Event Shirt(s)','Shirt Size (Included in Entry)','Regular T-Shirt OR Tech Shirt - Select Size/Type','shirt size (unisex cotton)')), oi7.answer, '') as shirt
    FROM ordermeta om
    JOIN orders o ON o.orderid=om.orderid
    LEFT JOIN orderitems oi ON oi.groupid=om.groupid AND oi.question = 'First Name'
    LEFT JOIN orderitems oi1 ON oi1.groupid=om.groupid AND oi1.question = 'Last Name'
    LEFT JOIN orderitems oi2 ON oi2.groupid=om.groupid AND oi2.question = 'City'
    LEFT JOIN orderitems oi3 ON oi3.groupid=om.groupid AND oi3.question = 'State'
    LEFT JOIN orderitems oi4 ON oi4.groupid=om.groupid AND oi4.question = 'Birthdate'
    LEFT JOIN orderitems oi5 ON oi5.groupid=om.groupid AND oi5.question = 'Gender'
    LEFT JOIN orderitems oi6 ON oi6.groupid=om.groupid AND oi6.question LIKE '%fee%'
    LEFT JOIN orderitems oi7 ON oi7.groupid=om.groupid AND oi7.question IN ('Youth Shirts','Event Shirt','Tech Shirt Size','Series Shirt','Choose Event Shirt(s)','Shirt Size (Included in Entry)','Regular T-Shirt OR Tech Shirt - Select Size/Type','shirt size (unisex cotton)')
    LEFT JOIN forms f ON f.fid=om.fid
    WHERE om.status = 1
        AND o.paid = 1
        AND f.fid=$fid
    ORDER BY lname ASC

这是一个非常难看的部分,因为衬衫尺寸条目是灵活的...将需要标准化条目,使查询只查找LIKE"衬衫尺寸%"

答案 1 :(得分:0)

SELECT  om.groupid, 
        om.fid, 
        om.eid, 
        f.subtitle,
        IF(oi.question = 'First Name', oi.answer, '') AS fname,
        IF(oi1.question = 'Last Name', oi1.answer, '') AS lname,
        IF(oi2.question = 'City', oi2.answer, '') AS city,
        IF(oi3.question = 'State', oi3.answer, '') AS state,
        IF(oi4.question = 'Birthdate', oi4.answer, '') AS bday,
        IF(oi5.question = 'Gender', oi5.answer, '') AS gender,
        IF ( (oi6.question LIKE '%fee'), oi6.answer, '') as event,
        IF ( (oi7.question = 'shirt size (unisex cotton)'), oi7.answer, '') as shirt
    FROM ordermeta om
    JOIN orders o ON o.orderid=om.orderid
    LEFT JOIN orderitems oi ON oi.groupid=om.groupid
    WHERE   oi.question IN( 'First NAME','Last Name','City','State','Birthdate','Gender')
        OR oi.question LIKE  '%fee%'
GROUP BY om.groupid, 
        om.fid, 
        om.eid, 
        f.subtitle,
        IF(oi.question = 'First Name', oi.answer, '') AS fname,
        IF(oi1.question = 'Last Name', oi1.answer, '') AS lname,
        IF(oi2.question = 'City', oi2.answer, '') AS city,
        IF(oi3.question = 'State', oi3.answer, '') AS state,
        IF(oi4.question = 'Birthdate', oi4.answer, '') AS bday,
        IF(oi5.question = 'Gender', oi5.answer, '') AS gender,
        IF ( (oi6.question LIKE '%fee'), oi6.answer, '') as event,
        IF ( (oi7.question = 'shirt size (unisex cotton)'), oi7.answer, '') as shirt
    ORDER BY lname ASC