带有变量字段的PHP mysql交叉表
我正在努力制作一张透视表/交叉表。最后,我想对其进行内联编辑,但首先我想至少生成表 在“tarifs”表中,我有一个Id、TarifCode和TarifDescr 比如: 在我的应用程序中的某个时刻,我填写了开始日期、结束日期以及适用的tarifcode的值(金额)。 比如: 提交后,SQL查询将填充一个表“occucion”,该表包含ID、日期、TarifCode、值,如:带有变量字段的PHP mysql交叉表,php,mysql,Php,Mysql,我正在努力制作一张透视表/交叉表。最后,我想对其进行内联编辑,但首先我想至少生成表 在“tarifs”表中,我有一个Id、TarifCode和TarifDescr 比如: 在我的应用程序中的某个时刻,我填写了开始日期、结束日期以及适用的tarifcode的值(金额)。 比如: 提交后,SQL查询将填充一个表“occucion”,该表包含ID、日期、TarifCode、值,如: 1, 2012-02-05, A, 1 2, 2012-02-05, V, 2 3, 2012-02-06, A, 1
1, 2012-02-05, A, 1
2, 2012-02-05, V, 2
3, 2012-02-06, A, 1
4, 2012-02-06, V, 2
5, 2012-02-07, A, 1
6, 2012-02-07, V, 2
7, 2012-02-08, A, 1
8, 2012-02-08, V, 2
9, 2012-02-09, A, 1
10, 2012-02-09, V, 2
我的问题是:
如何创建一个查询(或视图)以提供下一个输出:
-- 2012-02-05 | 2012-02-06 | 2012-02-07 | 2012-02-08 | 2012-02-09
A 1 1 1 1 1
V 2 2 2 2 2
在大多数与此主题相关的帖子中,这些值都是已知的。在我的例子中,有时没有使用TarifCode,或者创建了一个新的TarifCode
最后,我希望以JSON样式实现这一点,这样我就可以在网格中使用它进行内联编辑。也许有人对此有经验?如果您想使用SQL执行此操作,那么您可以使用聚合函数和
CASE
表达式在MySQL中透视数据。这将获取日期
值并将其转换为列:
select tarifcode,
max(case when Date = '2012-02-05' then value end) `2012-02-05`,
max(case when Date = '2012-02-06' then value end) `2012-02-06`,
max(case when Date = '2012-02-07' then value end) `2012-02-07`,
max(case when Date = '2012-02-08' then value end) `2012-02-08`,
max(case when Date = '2012-02-09' then value end) `2012-02-09`
from yourtable
group by tarifcode
看
如果日期未知,则可以使用类似以下内容的准备语句:
SET @sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'max(case when Date = ''',
Date,
''' then value end) AS `',
Date, '`'
)
) INTO @sql
FROM yourtable;
SET @sql = CONCAT('SELECT TarifCode, ', @sql, '
FROM yourtable
GROUP BY TarifCode');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
select
t.tarifcode,
max(case when Date = '2012-02-05' then value end) `2012-02-05`,
max(case when Date = '2012-02-06' then value end) `2012-02-06`,
max(case when Date = '2012-02-07' then value end) `2012-02-07`,
max(case when Date = '2012-02-08' then value end) `2012-02-08`,
max(case when Date = '2012-02-09' then value end) `2012-02-09`
from tarifs t
left join yourtable y
on t.tarifcode = y.tarifcode
group by t.tarifcode
看。两个查询的结果都是:
| TARIFCODE | 2012-02-05 | 2012-02-06 | 2012-02-07 | 2012-02-08 | 2012-02-09 |
------------------------------------------------------------------------------
| A | 1 | 1 | 1 | 1 | 1 |
| V | 2 | 2 | 2 | 2 | 2 |
编辑,如果要连接到另一个表,则可以使用类似以下内容:
SET @sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'max(case when Date = ''',
Date,
''' then value end) AS `',
Date, '`'
)
) INTO @sql
FROM yourtable;
SET @sql = CONCAT('SELECT TarifCode, ', @sql, '
FROM yourtable
GROUP BY TarifCode');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
select
t.tarifcode,
max(case when Date = '2012-02-05' then value end) `2012-02-05`,
max(case when Date = '2012-02-06' then value end) `2012-02-06`,
max(case when Date = '2012-02-07' then value end) `2012-02-07`,
max(case when Date = '2012-02-08' then value end) `2012-02-08`,
max(case when Date = '2012-02-09' then value end) `2012-02-09`
from tarifs t
left join yourtable y
on t.tarifcode = y.tarifcode
group by t.tarifcode
请参见感谢您的快速响应!我忘了什么,这就是我回来的原因。也许你能再给我一次提示?我想加入TarifCode表,所以所有的TarifCodes都被保留,所有的日期(在正手时确实未知)都在上面。仅填充具有值的字段。其他的都是空的。可能吗?再次感谢你的帮助!Roy@RoyVissers请查看我的编辑,您将只使用一个连接到另一个table@RoyVissers很高兴你找到了答案!:)