<?PHP
$query1 = "CREATE VIEW current_rankings AS SELECT * FROM main_table WHERE date = X";
$query2 = "CREATE VIEW prevIoUs_rankings AS SELECT rank FROM main_table WHERE date = date_sub('X', INTERVAL 1 MONTH)";
$query3 = "CREATE VIEW final_output AS SELECT current_rankings.player, current_rankings.rank as current_rank LEFT JOIN prevIoUs_rankings.rank as prev_rank
ON (current_rankings.player = prevIoUs_rankings.player)";
$query4 = "SELECT *, @rank_change = prev_rank - current_rank as rank_change from final_output";
$result = MysqL_query($query4) or die(MysqL_error());
while($row = MysqL_fetch_array($result)) {
echo $row['player']. $row['current_rank']. $row['prev_rank']. $row['rank_change'];
}
?>
所有查询都独立工作,但我真的很难将所有部分组合在一个结果中,所以我可以将它与MysqL_fetch_array一起使用.
我试图创建视图以及临时表,但每次它表示表不存在或返回一个空的fetch数组循环…逻辑是存在但语法搞砸了我认为这是我第一次不得不处理多个查询我需要合并在一起.期待一些支持.非常感谢.
解决方法:
感谢PHP.net我提出了一个解决方案:你必须使用(MysqLi_multi_query($link,$query))来运行多个连接查询.
/* create sql connection*/
$link = MysqLi_connect("server", "user", "password", "database");
$query = "sql STATEMENTS;"; /* first query : Notice the 2 semicolons at the end ! */
$query .= "sql STATEMENTS;"; /* Notice the dot before = and the 2 semicolons at the end ! */
$query .= "sql STATEMENTS;"; /* Notice the dot before = and the 2 semicolons at the end ! */
$query .= "sql STATEMENTS"; /* last query : Notice the dot before = at the end ! */
/* Execute queries */
if (MysqLi_multi_query($link, $query)) {
do {
/* store first result set */
if ($result = MysqLi_store_result($link)) {
while ($row = MysqLi_fetch_array($result))
/* print your results */
{
echo $row['column1'];
echo $row['column2'];
}
MysqLi_free_result($result);
}
} while (MysqLi_next_result($link));
}