【问题标题】:Multiple rows from same table to single row with like identifier从同一个表的多行到具有相似标识符的单行
【发布时间】:2015-05-27 07:33:30
【问题描述】:

我是 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 个)

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

我尝试了多种不同的方法,例如 CASE、IF THEN 语句。在我添加衬衫问题之前,它们都做同样的事情。

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

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

【问题讨论】:

    标签: mysql left-join multiple-columns


    【解决方案1】:

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

    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 "shirt size%"

    【讨论】:

      【解决方案2】:
      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
      

      【讨论】:

        猜你喜欢
        • 2022-01-15
        • 1970-01-01
        • 2019-01-14
        • 1970-01-01
        • 2018-03-13
        • 2011-04-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多