简体   繁体   English

尝试将多个查询合并为一个查询

[英]Trying to combine multiple queries into one query

*Note, if you can think of a more constructive title, please do edit. *注意,如果您能想到更具建设性的标题,请进行编辑。


I have a table that looks like this: 我有一个看起来像这样的表:

   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

Now, my problem is that I'm trying to seperate multiple queries into one: 现在,我的问题是我试图将多个查询分离为一个:

$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'");

(Don't worry about the $database->... part, that's my class.) (不用担心$ database-> ...部分,那是我的课程。)


How might I combine these into one? 我如何将它们组合成一个?

Thank you. 谢谢。

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

You could use "`OR'". 您可以使用“`OR”。

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

Or, you could use multiple query to execute multiple queries at the same time? 或者,您可以使用多个查询来同时执行多个查询?

http://php.net/manual/en/mysqli.multi-query.php http://php.net/manual/zh/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();
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM