简体   繁体   English

根据sql结果显示行

[英]Display rows based on sql results

I am using caldera forms in my WordPress site to collect data for new admissions for our club. 我正在我的WordPress网站中使用破火山口表单来收集有关我们俱乐部的新招生数据。 Form functions as expected. 窗体功能按预期方式。 I am trying to view a list of open applications and a page to view them. 我正在尝试查看打开的应用程序列表和查看它们的页面。 Following is my SQL table tor entries ( cf_form_entries ). 以下是我的SQL表Tor条目( cf_form_entries )。

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

Following table contains all the information submitted by the form ( cf_form_entry_values ); 下表包含表单( 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          |
+----+----------+---------------+--------------------+

I can run a simple select query and get the open application details of a given branch, inner joining tables. 我可以运行一个简单的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%'

Above query results in the following table; 上面的查询结果如下表所示;

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

My question is, How can I display (echo) the other details such as the name, email address, etc. of the selected tables? 我的问题是, 如何显示(回显)所选表的其他详细信息,例如名称,电子邮件地址等?

As such my end result shall display all details of the open applications in a table. 因此,我的最终结果将在表格中显示所有打开的应用程序的详细信息。 (not just the branch name) (不仅是分支名称)

I tried a while loop. 我尝试了一会儿循环。 But I can only echo the branch names as they are the only data selected in my inner joined table. 但是我只能回显分支名称,因为它们是我的内部联接表中唯一选择的数据。

One method is conditional aggregation: 一种方法是条件聚合:

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;

The nice thing about this approach is that adding a new value only requires adding a new expression in the SELECT . 这种方法的好处是,添加新值仅需要在SELECT添加新表达式。

Try Following Query By Using LEFT JOIN ON Two Tables. 尝试通过在两个表上使用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%'

This Might Be Helpful. 这可能会有所帮助。 And You Get Proper Output... 然后您会得到正确的输出...

This should give you the first two values, and you can use the same pattern for the rest: 这应该给您前两个值,其余的可以使用相同的模式:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM