MySql查询 - 多个SELECT语句

时间:2015-10-07 14:51:47

标签: mysql select join group-concat gravity-forms-plugin

我正在创建这个MYSQL查询:

SELECT
      GROUP_CONCAT(if(field_number = '2', value, NULL)) AS 'Field1',
      GROUP_CONCAT(if(field_number = '3', value, NULL)) AS 'Field2',
      GROUP_CONCAT(if(field_number = '4', value, NULL)) AS 'Field3',
      GROUP_CONCAT(if(field_number = '5', value, NULL)) AS 'Field4',
      GROUP_CONCAT(if(field_number = '6', value, NULL)) AS 'Field5',
      GROUP_CONCAT(if(field_number = '7', value, NULL)) AS 'Field6',
      GROUP_CONCAT(if(field_number = '8', value, NULL)) AS 'Field7',
      GROUP_CONCAT(if(field_number = '9', value, NULL)) AS 'Field8'
from wp_rg_lead_detail WHERE form_id = "3"
GROUP BY lead_id;

我对上述查询得到了很好的结果。我有另一个表存储每个条目的日期,可以通过form_id进行交叉检查。

我用来获取日期的查询如下:

SELECT date_created FROM wp_rg_lead WHERE form_id="3";

我需要加入两个查询,以便将所有结果合二为一。

1 个答案:

答案 0 :(得分:0)

由于您没有提供表格结构,我猜是这样的:

SELECT
      wp2.date_created,
      GROUP_CONCAT(if(field_number = '2', value, NULL)) AS 'Field1',
      GROUP_CONCAT(if(field_number = '3', value, NULL)) AS 'Field2',
      GROUP_CONCAT(if(field_number = '4', value, NULL)) AS 'Field3',
      GROUP_CONCAT(if(field_number = '5', value, NULL)) AS 'Field4',
      GROUP_CONCAT(if(field_number = '6', value, NULL)) AS 'Field5',
      GROUP_CONCAT(if(field_number = '7', value, NULL)) AS 'Field6',
      GROUP_CONCAT(if(field_number = '8', value, NULL)) AS 'Field7',
      GROUP_CONCAT(if(field_number = '9', value, NULL)) AS 'Field8'
FROM wp_rg_lead_detail wp1
LEFT JOIN wp_rg_lead wp2
ON wp1.lead_id = wp2.id
WHERE wp1.form_id = "3"
GROUP BY wp1.lead_id;