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
问题在哪里?帮助我。
答案 0 :(得分:5)
您可以使用游标获取t
表上的每个REPORT_QUERY
,并使用预准备语句执行该操作:
CT
创建程序后,您可以调用如下:
调用run_queries();
那就是它。
答案 1 :(得分:1)
如果要执行存储在某些MySQL表的列中的SQL,请尝试以下操作:
SELECT sql_text INTO @sql FROM sql_queries WHERE id = 1;
PREPARE sql_query FROM @sql;
EXECUTE sql_query;
表sql_queries
的内容:
+----+--------------------------+ | id | sql_text | +----+--------------------------+ | 1 | select * from companies; | +----+--------------------------+
答案 2 :(得分:0)
据我所知,您的问题是您需要运行table column
表格中存储在表格列中的查询。
在这里,您可以从<?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;
}
?>
获取查询并在mysqli中执行它们。
SQL EMPLOYEE TABLE [在执行期间由查询表动态调用]
PHP SQL代码:`
curl 'https://doc-0s-80-docs.googleusercontent.com/docs/securesc/aa51s66fhf9273i....................blah blah blah...............gEIqZ3KAQ==' --compressed
`