duanguilin2007 2013-07-22 04:20
浏览 56
已采纳

如何在PHP中实现mySQL动态查询?

I can produce this output:

+----------+------------+------------+------------+
| startt   | 2013-04-01 | 2013-04-02 | 2013-04-03 |
+----------+------------+------------+------------+
| 08:00:00 | Donald     | Daisy      | Mickey     |
| 12:00:00 | Pluto      | Goofy      | Minnie     |
| 14:00:00 | NULL       | Mickey     | NULL       |
+----------+------------+------------+------------+

from this original data:

mysql> select * from test;
+------------+----------+----------+--------+
| startd     | startt   | duration | name   |
+------------+----------+----------+--------+
| 2013-04-01 | 08:00:00 |        4 | Donald |
| 2013-04-02 | 08:00:00 |        4 | Daisy  |
| 2013-04-03 | 08:00:00 |        4 | Mickey |
| 2013-04-03 | 12:00:00 |        4 | Minnie |
| 2013-04-01 | 12:00:00 |        4 | Pluto  |
| 2013-04-02 | 12:00:00 |        4 | Goofy  |
| 2013-04-02 | 14:00:00 |        4 | Mickey |
+------------+----------+----------+--------+
mysql>

Using this MySQL dynamic query:

1 set @sql = null;
2 select
3     group_concat(distinct
4         concat(
5             'group_concat(case when startd = ''',
6             `startd`,
7             ''' then `name` end ) as `',
8             `startd`,'`'
9         )
10     ) into @sql
11 from test;
12
13 set @sql = concat('select startt, ',@sql,'
14                     from test
15                     group by startt');
16
17 prepare stmt from @sql;
18 execute stmt;
19 deallocate prepare stmt;

Thanks for your help to this point @hims056.

How can I pass the results of this dynamic query to a variable that I can loop over in PHP?

In the past I have used:

$result=mysqli_query($con,"select ...");
... lines deleted ...
while ($row=mysqli_fetch_array($result))
... lines deleted ...

This method does not seem appropriate in these circumstances.

Any assistance would be appreciated.

  • 写回答

1条回答 默认 最新

  • douzhengyi5022 2013-07-22 04:33
    关注

    A possible solution is to wrap it in a stored procedure

    DELIMITER $$
    CREATE PROCEDURE sp_test()
        BEGIN
        SET @sql = NULL;
        SELECT
            GROUP_CONCAT(DISTINCT
                CONCAT(
                    'GROUP_CONCAT(CASE WHEN startd = ''',
                    `startd`,
                    ''' THEN `name` END ) AS `',
                    `startd`,'`'
                 )
             ) INTO @sql
         FROM test;
    
         SET @sql = CONCAT('SELECT startt, ', @sql, '
                              FROM test
                             GROUP BY startt');
    
        PREPARE stmt FROM @sql;
        EXECUTE stmt;
        DEALLOCATE PREPARE stmt;
    END$$
    DELIMITER ;
    

    And use it

    CALL sp_test();
    

    Here is SQLFiddle demo

    UPDATE: on php side you can do

    $db = new mysqli('localhost', 'user', 'password', 'dbname');
    $sql = "CALL sp_test()";
    $query = $db->query($sql);
    $result = array();
    while ($row = $query->fetch_assoc()) {
        $result[] = $row;
    }
    $query->close();
    $db->close();
    // then do whatever you need to do to present it
    var_dump($result);
    

    All error handling has been intentionally omitted for brevity

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像