如何用PHP显示事务SQL动态数据结果?

时间:2016-05-12 11:33:43

标签: php sql

此问题旁边:MSSQL 2012 : PIVOT typologies and dates using PHP我将此结果导入Management Studio:

typo    01/04/2016 | 02/04/2016 | 04/04/2016 | 05/04/2016 | 06/04/2016 | 07/04/2016 | 08/04/2016 | 11/04/2016 |
TYPO1       0      |     0      |     0      |     0      |     0      |     0      |     0      |     0      |
TYPO2       13     |     0      |     29     |     30     |     24     |     18     |     17     |     22     |     
TYPO3       0      |     0      |     0      |     0      |     0      |     0      |     0      |     0      |     
TYPO4       6      |     1      |     12     |     16     |     17     |     6      |     18     |     11     |     
TYPO5       44     |     1      |     80     |     62     |     84     |     116    |     103    |     100    |     

如何在PHP中显示此结果?我从:

开始
$SQL = "
DECLARE @cols NVARCHAR (MAX);

SELECT @cols = COALESCE (@cols + ',[' + CONVERT(NVARCHAR, [DATE], 103) + ']', 
               '[' + CONVERT(NVARCHAR, [DATE], 103) + ']')
               FROM    (SELECT DISTINCT [DATE] FROM (SELECT DISTINCT CAST(dateadd(S, [date_ticket], '1970-01-01') as date) AS [DATE] FROM [MyBase].[dbo].[ticket] WHERE date_ticket BETWEEN 1459461600 AND 1462053600) PV) PT
               ORDER BY [DATE];

DECLARE @query NVARCHAR(MAX);

SET @query = 'SELECT * FROM (
SELECT
    typo_1,
    CAST(dateadd(S, [date_ticket], ''1970-01-01'') as date) AS [DATE]
FROM 
    [MyBase].[dbo].[ticket] 
WHERE 
    date_ticket BETWEEN 1459461600 AND 1462053600
) X
PIVOT (
    COUNT([DATE]) FOR [DATE] IN ('+@cols+')
) p';

EXEC SP_EXECUTESQL @query;";

$result = sqlsrv_query($connexion, $SQL);

此结果基于4月,日期可随用户的选择(所有月份或3天等)而变化;所以我想对此进行Excel导出但是如何构造输出表呢?我必须先为每个日期对每一列进行循环吗? PHP中有没有办法复制这个结果表并将其粘贴到Excel文件中?

0 个答案:

没有答案