7
<?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 数组循环......逻辑在那里但语法混乱我认为这是我第一次不得不这样做处理我需要合并在一起的多个查询。期待一些支持。非常感谢。

4

3 回答 3

22

感谢 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));
}

编辑 - 如果您真的想做一个大查询,上述解决方案可以工作,但也可以执行任意数量的查询并单独执行它们。

$query1 = "Create temporary table A select c1 from t1"; 
$result1 = mysqli_query($link, $query1) or die(mysqli_error());

$query2 = "select c1 from A"; 
$result2 = mysqli_query($link, $query2) or die(mysqli_error());

while($row = mysqli_fetch_array($result2)) {

echo $row['c1'];
    }  
于 2012-05-19T10:16:06.323 回答
0

看来您没有执行 $query1 - $query3。您刚刚跳到 $query4 如果其他人没有先执行,它将无法工作。

$query4 = "SELECT *, @rank_change = prev_rank - current_rank as rank_change from final_output";

应该是

$query4 = "SELECT *, @rank_change := prev_rank - current_rank as rank_change from final_output";

否则 rank_change 的值将只是一个布尔值,如果 @rank_change 等于 (prev_rank - current_rank) 则为 true,否则为 false。但是你需要@rank_change 吗?您会在后续查询中使用它吗?也许你可以完全删除它。

更好的是,您可以像这样将所有查询组合成一个:

SELECT 
    curr.player,
    curr.rank AS current_rank,
    @rank_change := prev.rank - curr.rank AS rank_change
FROM
    main_table AS curr
    LEFT JOIN main_table AS prev
        ON curr.player = prev.player    
WHERE 
    curr.date = X
    AND prev.date = date_sub('X', INTERVAL 1 MONTH)
于 2012-05-16T00:49:20.247 回答
-1

你应该连接它们:

<?php

$query = "CREATE VIEW current_rankings AS SELECT * FROM main_table WHERE date = X";

$query .= " CREATE VIEW previous_rankings AS SELECT rank FROM main_table WHERE date =     date_sub('X', INTERVAL 1 MONTH)";

$query .= " 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)";

$query .= " SELECT *, @rank_change = prev_rank - current_rank as rank_change from final_output";

$result = mysql_query($query) or die(mysql_error()); 

while($row = mysql_fetch_array($result)) {
echo $row['player']. $row['current_rank']. $row['prev_rank']. $row['rank_change'];
}

?>
于 2012-05-16T00:56:33.643 回答