php / mysql有多个查询

时间:2012-05-16 00:44:49

标签: php mysql

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

3 个答案:

答案 0 :(得分:13)

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

答案 1 :(得分: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'];
}

?>

答案 2 :(得分: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)