尝试将多个查询组合到一个查询中

时间:2013-04-28 04:56:14

标签: php mysql

*请注意,如果您能想到更具建设性的标题,请进行编辑。


我有一个看起来像这样的表:

   id   |   forGame    |   otherType    |   otherName    |   otherDesc
9       |+Stellar+Dawn |Character       |Car             |Car
10      |+Stellar+Dawn |Item            |Brugson Burson  |a guy
11      |+Stellar+Dawn |Item            |Space Pie       |A pie from space

现在,我的问题是我正在尝试将多个查询分成一个:

$gameOther_typeItem = $database->queryDB("SELECT * FROM `mainSite_others` WHERE otherType='Item' AND forGame='$gameName'");
$gameOther_typeEntity = $database->queryDB("SELECT * FROM `mainSite_others` WHERE otherType='Entity' AND forGame='$gameName'");
$gameOther_typeCharacter = $database->queryDB("SELECT * FROM `mainSite_others` WHERE otherType='Character' AND forGame='$gameName'");

(不要担心$ database-> ...部分,这是我的班级。)


我如何将这些组合成一个?

谢谢。

2 个答案:

答案 0 :(得分:0)

  

SELECT * FROM mainSite_others WHERE(otherType ='Item'或otherType =   'Entity'或otherType ='Character')AND forGame ='$ gameName';

答案 1 :(得分:0)

您可以使用“OR”。

SELECT * FROM my_table WHERE column1 = value1 AND column3 = value3 OR column2 = value2 AND column3 = value3

或者,您可以使用多个查询同时执行多个查询吗?

http://php.net/manual/en/mysqli.multi-query.php

<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$query  = "SELECT CURRENT_USER();";
$query .= "SELECT Name FROM City ORDER BY ID LIMIT 20, 5";

/* execute multi query */
if ($mysqli->multi_query($query)) {
    do {
        /* store first result set */
        if ($result = $mysqli->store_result()) {
            while ($row = $result->fetch_row()) {
                printf("%s\n", $row[0]);
            }
            $result->free();
        }
        /* print divider */
        if ($mysqli->more_results()) {
            printf("-----------------\n");
        }
    } while ($mysqli->next_result());
}

/* close connection */
$mysqli->close();
?>