根据sql结果显示行

时间:2016-12-17 12:20:25

标签: php mysql sql

我在我的WordPress网站上使用破火山口表格收集我们俱乐部新招生的数据。表格按预期运作。我正在尝试查看打开的应用程序列表和查看它们的页面。以下是我的SQL表格条目(cf_form_entries)。

+----+-----------------+--------+
| ID | Form ID         | Status |
+----+-----------------+--------+
| 1  | CF5852c23e56b1d | active |
+----+-----------------+--------+
| 2  | CF5852c23e56b1d | active |
+----+-----------------+--------+
| 3  | CF5852c23e56b1d | active |
+----+-----------------+--------+

下表包含表单(cf_form_entry_values);

提交的所有信息
+----+----------+---------------+--------------------+
| id | entry_id |     slug      |       value        |
+----+----------+---------------+--------------------+
|  1 |        1 | branch        | Branch A           |
|  2 |        1 | full_name     | asdasd asdasd      |
|  3 |        1 | email_address | b2196363@trbvn.com |
|  4 |        1 | phone         | 111111111          |
|  5 |        2 | branch        | Branch A           |
|  6 |        2 | full_name     | Full Name          |
|  7 |        2 | email_address | asdasd@asdas.com   |
|  8 |        2 | phone         | 111111111          |
|  9 |        3 | branch        | Branch A           |
| 10 |        3 | full_name     | Namwe              |
| 11 |        3 | email_address | wert@wertwert.com  |
| 12 |        3 | phone         | 111111111          |
+----+----------+---------------+--------------------+

我可以运行一个简单的select查询,并获取给定分支的打开应用程序详细信息,内部连接表。

SELECT cf_form_entries.id,
       cf_form_entries.form_id,
       cf_form_entries.status,
       cf_form_entry_values.slug,
       cf_form_entry_values.value
FROM cf_form_entry_values
  INNER JOIN cf_form_entries
     ON cf_form_entry_values.entry_id = cf_form_entries.id
WHERE cf_form_entry_values.slug = 'branch'
   AND cf_form_entry_values.value LIKE '%Branch A%'

以上查询结果如下表所示;

+----+-----------------+--------+--------+----------+
| id |     form_id     | status |  slug  |  value   |
+----+-----------------+--------+--------+----------+
|  1 | CF5852c23e56b1d | active | branch | Branch A |
|  3 | CF5852c23e56b1d | active | branch | Branch A |
+----+-----------------+--------+--------+----------+

我的问题是,如何显示(回显)其他详细信息,例如所选表格的名称,电子邮件地址等?

因此,我的最终结果将在表格中显示打开的应用程序的所有详细信息。 (不只是分支名称)

我尝试了一个while循环。但我只能回显分支名称,因为它们是我内部联接表中选择的唯一数据。

3 个答案:

答案 0 :(得分:2)

一种方法是条件聚合:

SELECT fe.id, fe.form_id, fe.status,
       MAX(CASE WHEN fev.slug = 'branch' THEN fev.value END) as branch,
       MAX(CASE WHEN fev.slug = 'full_name' THEN fev.value END) as full_name,
       . . .
FROM cf_form_entries fe INNER JOIN
     cf_form_entry_values fev
     ON fev.entry_id = fe.id
GROUP BY fe.id, fe.form_id, fe.status;

这种方法的好处是添加新值只需要在SELECT中添加新表达式。

答案 1 :(得分:1)

在两个表上使用LEFT JOIN尝试执行以下查询。

SELECT cfev.*,cfm.Form ID as entry_id 
     FROM cf_form_entry_values cfev 
         LEFT JOIN cf_form_entries cfm ON cfm.id = cfev.entry_id 
             WHERE cfev .slug = 'branch' AND vfm.value LIKE '%Branch A%'

这可能会有所帮助。你得到适当的输出......

答案 2 :(得分:1)

这应该给你前两个值,你可以使用相同的模式:

SELECT entries.id,
       entries.form_id,
       entries.status,
       (SELECT value FROM cf_form_entry_values as v 
        WHERE slug='branch' and entry_id=entries.entry_id) as branch,
       (SELECT value FROM cf_form_entry_values as v 
        WHERE slug='full_name' and entry_id=entries.entry_id) as full_name
FROM cf_form_entry_values
INNER JOIN (
  SELECT * FROM cf_form_entry_values
  INNER JOIN cf_form_entries
    ON cf_form_entry_values.entry_id = cf_form_entries.id
  WHERE cf_form_entry_values.slug = 'branch'
    AND cf_form_entry_values.value LIKE '%Branch A%'
  ) as entries
  ON cf_form_entry_values.entry_id = entries.id
GROUP BY cf_form_entries.id