8
mysql> select * from CT;

| CID | MID | REPORT_QUERY           |
|   1 |   1 | select * from emp;     |
|   2 |   2 | select * from student; |

2 rows in set (0.00 sec)

我想在REPORT_QUERY列中执行查询。

DELIMITER //
 CREATE PROCEDURE TRYct()
   BEGIN
SET @str=(SELECT GROUP_CONCAT(REPORT_QUERY SEPARATOR ' ') FROM CT);
PREPARE q from @str;
EXECUTE q;
   END //
 DELIMITER ;

我使用此代码,但如果我的表中只有一个查询,它就可以工作。如果有两个查询比它给出一个错误。

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'select * from student' at line 1

问题出在哪里 ?帮我。

4

3 回答 3

8

您可以使用游标来获取每个REPORT_QUERYCT,并使用准备好的语句执行它:

delimiter $$
drop procedure if exists run_queries$$
create procedure run_queries()
begin

    declare s_query varchar(255);

    declare done bool default false;
    declare c_queries cursor for    
        select REPORT_QUERY from CT;
    declare continue handler for not found set done = true;


    open c_queries;
    read_loop: loop

        fetch c_queries into s_query;
        if done then 
            leave read_loop;
        end if;

        -- run the query
        set @sql = s_query;
        prepare stmt from  @sql;
        execute stmt;
        deallocate prepare stmt;
    end loop;

end$$

创建过程后,您可以如下调用:

调用 run_queries();

而已。

于 2017-05-06T11:42:11.173 回答
4

如果要执行存储在某个 MySQL 表的列中的 SQL,请尝试以下操作:

SELECT sql_text INTO @sql FROM sql_queries WHERE id = 1;
PREPARE sql_query FROM @sql;
EXECUTE sql_query;

表格内容sql_queries

+----+--------------+
| 编号 | sql_text |
+----+--------------+
| 1 | 从公司中选择 *;|
+----+--------------+
于 2018-07-13T06:03:51.483 回答
0

据我了解,您的问题是您需要运行存储在SQLForm 的表列中的查询。

在这里,您可以获取查询表单table column并在 mysqli 中执行它们。

SQL QUERY TABLE IMAGE 这里存储了查询查询表图像

SQL 员工表 [在执行期间由查询表动态调用]

员工表

PHP SQL 代码:`

<?php
    $link = new mysqli ('localhost','root','admin','demo');

    if($link->connect_error){
        die ("Connection Failed".$link->connect_error);
    }

    //YOU NEED THIS AS YOU NEED TO ACCESS THE COLUMN DATA IN TABLE
    $sql = "SELECT * FROM querytable WHERE id=1";

    if($res = $link->query($sql)){
        //IF THE ROW EXISTS
        if($res->num_rows > 0){
            while($row = $res->fetch_assoc()){

                $query = $row['report_query']; //HERE IS THE SQL QUERY STORED IN TABLE , WHICH IS STORED IN ANOTHER VARIABLE
                $result = $link->query($query); // NOW EXECUTE THE QUERY
                if($result->num_rows > 0){
            ?>
                    <table border="1" cellpadding="10">
                        <tr>
                            <th>First Name</th>
                            <th>Last Name</th>
                        </tr>
            <?php
                    while($rows = $result->fetch_assoc()){
                        //DISPLAY RESULTS HERE
            ?>          
                        <tr>
                            <td><?php echo $rows['first_name'];?></td>
                            <td><?php echo $rows['lastname'];?></td>
                        </tr>
            <?php
                    }
            ?>
                    </table>
        <?php
                }
            }
        }   
    }
    else
    {
        echo $link->error;
    }
?>

`

于 2017-05-06T03:09:11.763 回答