需要使用WPDataTables将行转换为列以查询MySQL数据库

时间:2017-09-15 07:22:54

标签: mysql wordpress

我正在使用一个WordPress插件,它允许我从我的WordPress数据库中提取数据。问题是DB是由表单插件创建的,表结构是固定的:

+--------------+------------------+
|Column        |Type              |
+--------------+------------------+
|submit_time   |decimal(16,4)     |
+--------------+------------------+
|form_name     |varchar(127) NULL |
+--------------+------------------+
|field_name    |varchar(127) NULL |
+--------------+------------------+
|field_value   |longtext NULL     |
+--------------+------------------+
|field_order   |int(11) NULL      |
+--------------+------------------+
|file          |longblob NULL     |
+--------------+------------------+

因此,DB中出现的值不适合阅读或编辑。

+------------+------------+-----------------+------------------+------------+-----+
|submit_time |form_name   |field_name       | field_value      |file_order  |file | 
+------------+------------+-----------------+------------------+------------+-----+
|15052703120 |Submissions |your-name        | Leonard Chia     |9999        |file | 
+------------+------------+-----------------+------------------+------------+-----+
|15052703120 |Submissions |your-email       | leonard@mail     |8           |file | 
+------------+------------+-----------------+------------------+------------+-----+
|15052703120 |Submissions |status           | Pending Start    |9           |file | 
+------------+------------+-----------------+------------------+------------+-----+
|15052703120 |Submissions |location         | DD1 Classroom    |7           |file | 
+------------+------------+-----------------+------------------+------------+-----+

我试图实现这个目标:

+-------------+-------------+---------------+---------------+
| Requestor   | Email       | Location      | Status        |
+-------------+-------------+---------------+---------------+
|Leonard Chia | leonard@mail| DD1 Classroom | Pending Start |
+-------------+-------------+---------------+---------------+
|Sng Yeekia   | yeekias@mail| DD2 Classroom | Pending Spare |
+-------------+-------------+---------------+---------------+
|Gabriel Lee  | gabriel@mail| SL1 Classroom | Completed     |
+-------------+-------------+---------------+---------------+

我尝试使用CASE WHEN IF转置或转动表格,但它不起作用。还尝试使用MAXUNION ALL,但结果相同。

这里的任何人都可以向我指出我的编码有什么问题吗?

这是我的代码:

SELECT wpc5_cf7dbplugin_submits.submit_time
   MAX (IF(wpc5_cf7dbplugin_submits.field_name = 'your-name', wpc5_cf7dbplugin_submits.field_value,NULL)) AS Requestor,
   MAX (IF(wpc5_cf7dbplugin_submits.field_name = 'campus', wpc5_cf7dbplugin_submits.field_value,NULL)) AS Campus,
   MAX (IF(wpc5_cf7dbplugin_submits.field_name = 'location', wpc5_cf7dbplugin_submits.field_value,NULL)) AS Defect Location,
   MAX (IF(wpc5_cf7dbplugin_submits.field_name = 'your-message', wpc5_cf7dbplugin_submits.field_value,NULL)) AS Defect Description,
   MAX (IF(wpc5_cf7dbplugin_submits.field_name = 'menu-priority', wpc5_cf7dbplugin_submits.field_value,NULL)) AS Priority,
   MAX (IF(wpc5_cf7dbplugin_submits.field_name = 'work-done', wpc5_cf7dbplugin_submits.field_value,NULL)) AS Action Taken,
   MAX (IF(wpc5_cf7dbplugin_submits.field_name = 'assigned-to', wpc5_cf7dbplugin_submits.field_value,NULL)) AS Assigned To,
   MAX (IF(wpc5_cf7dbplugin_submits.field_name = 'category', wpc5_cf7dbplugin_submits.field_value,NULL)) AS Category,
   MAX (IF(wpc5_cf7dbplugin_submits.field_name = 'file-photo', wpc5_cf7dbplugin_submits.field_value,NULL)) AS Photo
   MAX (IF(wpc5_cf7dbplugin_submits.field_name = 'status', wpc5_cf7dbplugin_submits.field_value,NULL)) AS Status
FROM wpc5_cf7dbplugin_submits
GROUP BY wpc5_cf7dbplugin_submits.submit_time

wpdatatables返回的错误:

  

未找到任何结果。请检查此查询是否正确! Table构造函数需要一个返回数据以构建wpDataTable的查询。   错误:SQL语法中有错误;检查与MySQL服务器版本对应的手册,以便在'MAX(IF(wpc5_cf7dbplugin_submits.field_name ='your-name',wpc5_cf7dbplugin_subm'第1行)附近使用正确的语法

更新

编辑下面的代码

    SELECT wpc5_cf7dbplugin_submits.submit_time,
   CASE WHEN wpc5_cf7dbplugin_submits.field_name = 'your-name' THEN wpc5_cf7dbplugin_submits.field_value ELSE NULL END AS Requestor,
   CASE WHEN wpc5_cf7dbplugin_submits.field_name = 'campus' THEN wpc5_cf7dbplugin_submits.field_value ELSE NULL END AS Campus,
   CASE WHEN wpc5_cf7dbplugin_submits.field_name = 'location' THEN wpc5_cf7dbplugin_submits.field_value ELSE NULL END AS Location,
   CASE WHEN wpc5_cf7dbplugin_submits.field_name = 'your-message' THEN wpc5_cf7dbplugin_submits.field_value ELSE NULL END AS Description,
   CASE WHEN wpc5_cf7dbplugin_submits.field_name = 'menu-priority' THEN wpc5_cf7dbplugin_submits.field_value ELSE NULL END AS Priority,
   CASE WHEN wpc5_cf7dbplugin_submits.field_name = 'work-done' THEN wpc5_cf7dbplugin_submits.field_value ELSE NULL END AS Action,
   CASE WHEN wpc5_cf7dbplugin_submits.field_name = 'assigned-to' THEN wpc5_cf7dbplugin_submits.field_value ELSE NULL END AS Assigned,
   CASE WHEN wpc5_cf7dbplugin_submits.field_name = 'category' THEN wpc5_cf7dbplugin_submits.field_value ELSE NULL END AS Category,
  CASE WHEN wpc5_cf7dbplugin_submits.field_name = 'file-photo' THEN wpc5_cf7dbplugin_submits.field_value ELSE NULL END AS Photo,
  CASE WHEN wpc5_cf7dbplugin_submits.field_name = 'status' THEN wpc5_cf7dbplugin_submits.field_value ELSE NULL END AS Status
FROM wpc5_cf7dbplugin_submits
GROUP BY wpc5_cf7dbplugin_submits.submit_time

表中有更多的NULL然后是字段,大多数填充的字段只来自第1列,这很奇怪。

+-------------+----------+----------+----------+
| Requestor   | Campus   | Location | Status   |
+-------------+----------+----------+----------+
|Leonard Chia | NULL     | NULL     | NULL     |
+-------------+----------+----------+----------+
|NULL         | NULL     | NULL     | NULL     |
+-------------+----------+----------+----------+
|Gabriel Lee  | NULL     | NULL     | NULL     |
+-------------+----------+----------+----------+

出了什么问题?

1 个答案:

答案 0 :(得分:0)

您的原始查询对我来说是正确的,所以我决定在以下演示中对其进行测试:

Demo

我发现了两个导致编译器错误的问题:

  1. 您使用的是由两个单词组成的别名但您没有逃脱它们。而不是使用AS Defect Location在包含别名的字词周围添加反引号:AS `Defect Location`

  2. 您的查询会在MAX和左括号之间放置空格,例如MAX (IF(condition, ...)。令我惊讶的是,当我删除此空间时,查询开始工作。

  3. 以下是更正这些错误的原始查询:

    SELECT
        submit_time,
        MAX(IF(field_name = 'your-name',     field_value, NULL)) AS Requestor,
        MAX(IF(field_name = 'campus',        field_value, NULL)) AS Campus,
        MAX(IF(field_name = 'location',      field_value, NULL)) AS `Defect Location`,
        MAX(IF(field_name = 'your-message',  field_value, NULL)) AS `Defect Description`,
        MAX(IF(field_name = 'menu-priority', field_value, NULL)) AS Priority,
        MAX(IF(field_name = 'work-done',     field_value, NULL)) AS `Action Taken`,
        MAX(IF(field_name = 'assigned-to',   field_value, NULL)) AS `Assigned To`,
        MAX(IF(field_name = 'category',      field_value, NULL)) AS Category,
        MAX(IF(field_name = 'file-photo',    field_value, NULL)) AS Photo
        MAX(IF(field_name = 'status',        field_value, NULL)) AS Status
    FROM
        wpc5_cf7dbplugin_submits
    GROUP BY
        submit_time
    

    请注意,在查询单个表时,您不需要重复完整的表名。