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

标签 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)

我试图让它看起来像:

表格确认(所需输出):

组ID |姓氏 |名字 |城市 | ... |衬衫尺码

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

abc000 |美国能源部 |约翰 |丹佛 | ... |超大号

abc001 |美国能源部 |简|西雅图 | ... |小

表格确认(实际输出):

组ID |姓氏 |名字 |城市 | ... |衬衫尺码

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

abc000 |美国能源部 |约翰 |丹佛 | ... |

abc000 |美国能源部 |约翰 |丹佛 | ... |超大号

abc001 |美国能源部 |简|西雅图 | ... |

查询是:

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" 

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

当前代码,当问题=“衬衫尺寸”存在时,我得到两个条目(一个是空白的,另一个有良好的信息)。所有其他条目都有一个条目,没有任何信息。 (140 个结果中的 195 个)

我尝试了多种不同的方法,例如 CASE、IF THEN 语句。他们都做同样的事情,效果很好,直到我添加衬衫问题。

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

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

最佳答案

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

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“衬衫尺码%”

关于mysql - 从同一个表到具有相似标识符的单行的多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29225020/

相关文章:

mysql - 在 SQL 查询中组合 "replace"和通配符

MySQL 5 更新后触发器不起作用

mysql - 从 2 个表中检索数据时,LEFT JOIN 未按预期工作

Android:创建多列按钮,每列都有独立的 ScrollView

替换 data.frame 列中的某些值

php - 无法创建表

MySQL:返回多个匹配项

mysql - 左连接 OR 子句

php - 如何在 Laravel 中使用 Left Join 查找匹配记录

linux - 每第 n 行将行转换为列