我使用breezingform为joomla创建web表单,它将用户输入记录存储在数据库中,如下所示:
forms_records
id | form | name
24 19 kpi
25 19 kpi
forms_subrecords
id | element | record | description | value | type
101 406 24 month 2015-01 calendar
102 407 24 east 30% text
103 408 24 south 35% text
104 409 24 north 50% text
105 410 24 west 25% text
106 406 25 month 2015-02 calendar
107 407 25 east 45% text
108 408 25 south 35% text
109 409 25 north 15% text
110 410 25 west 5% text
我试过这样做: 1.首先获得记录ID
SELECT id FROM forms_records WHERE form='19'
SELECT s.value from forms_subrecords AS s WHERE s.record IN (SELECT id FROM forms_records WHERE form='19')
结果是10行,一列,
2015-01
30%
35%
50%
25%
2015-02
45%
35%
15%
5%
但我想要的是2行5列:
2015-01 30% 35% 50% 25%
2015-02 45% 35% 15% 5%
任何人都可以提供帮助或提供一些提示吗?
答案 0 :(得分:0)
也许Pivot会起作用。希望能帮助到你。 http://en.wikibooks.org/wiki/MySQL/Pivot_table
答案 1 :(得分:0)
您可以将条件案例表达式与聚合最大函数一起使用来执行此操作:
SELECT
max(case when s.description = 'month' then s.value end) col1,
max(case when s.description = 'east' then s.value end) col2,
max(case when s.description = 'south' then s.value end) col3,
max(case when s.description = 'north' then s.value end) col4,
max(case when s.description = 'west' then s.value end) col5
FROM forms_subrecords AS s
WHERE s.record
IN (SELECT id FROM forms_records WHERE form='19')
GROUP BY
record
示例SQL Fiddle提供此输出:
| COL1 | COL2 | COL3 | COL4 | COL5 |
|---------|------|------|------|------|
| 2015-01 | 30% | 35% | 50% | 25% |
| 2015-02 | 45% | 35% | 15% | 5% |