1

我在 php 页面中使用了两个 select 语句,如下所示:

$num1 = mysql_query("SELECT SId FROM mtable WHERE Pathogen='$pathogen1' && Topic='$topic1' && Indicator='$ind1' && IndicatorSubGroup='$subind1' ");

$num2 = mysql_query("SELECT SId FROM mtable WHERE Pathogen='$pathogen2' && Topic='$topic2' && Indicator='$ind2' && IndicatorSubGroup='$subind2' ");

目前,结果使用单独的 while 循环显示,如下所示

while($nval=  mysql_fetch_array($num1))
{
    $vv=$nval['SId'];

    $result = mysql_query("SELECT * FROM mtable WHERE SId='$vv'");

    while($row = mysql_fetch_array($result))
    {
        print "<tr height='18'>";
        print "<td width=200 align=left>" . $row['Country'] . "</td>";
        print "<td width=70 align=center>" . $row['MidEstimate'] . "</td>";
        print "<td width=70 align=center>" . $row['LowEstimate'] . "</td>";
        print "<td width=70 align=center>" . $row['HighEstimate'] . "</td>";
        print "<td width=89 align=center>" . $row['StudyLocation'] . "</td>";
        print "<td width=139 align=center>" . $row['ReferenceID'] . "</td>";
        print "<td width=89 align=center>" . $row['Quality'] . "</td>";
        print "<td width=89 align=center>" . $row['Relevance'] . "</td>";
        print "<td width=61><a href='/sites/default/files/popupboxCD.php?SId=$vv' onClick='openpopup(this.href);return false;'>".$row['Info']."</a></td>";
        print "</tr>";
        print "<tr height='1'><td colspan='9' style='padding:0'><hr size='0.5' color='#CCCCCC' noshade='noshade' /></td></tr>";
    }
}

$num2 = mysql_query("SELECT SId FROM mtable WHERE Pathogen='$pathogen2' && Topic='$topic2' && Indicator='$ind2' && IndicatorSubGroup='$subind2' ");
print $num2;

print "<table border='0' width='875' align='center'>

<tr><td colspan=5 height=10></td></tr>
<tr>
    <td width='200' bgcolor='#99CCF5' align='center'><b>Country</b></td>
    <td width='70' bgcolor='#99CCF5' align='center'><b>Mid</b></td>
    <td width='70' bgcolor='#99CCF5' align='center'><b>Low</b></td>
    <td width='70' bgcolor='#99CCF5' align='center'><b>High</b></td>
    <td width='89' bgcolor='#99CCF5' align='center'><b>Study Location</b></td>
    <td width='139' bgcolor='#99CCF5' align='center'><b>Reference</b></td>
    <td width='89' bgcolor='#99CCF5' align='center'><b>Quality</b></td>
    <td width='89' bgcolor='#99CCF5' align='center'><b>Relevance</b></td>
    <td width='61' ></td>
</tr>";

while($nval=  mysql_fetch_array($num2))
{
    $vv=$nval['SId'];

    $result = mysql_query("SELECT * FROM mtable WHERE SId='$vv'");

    while($row = mysql_fetch_array($result))
    {
        print "<tr height='18'>";
        print "<td width=200 align=left>" . $row['Country'] . "</td>";
        print "<td width=70 align=center>" . $row['MidEstimate'] . "</td>";
        print "<td width=70 align=center>" . $row['LowEstimate'] . "</td>";
        print "<td width=70 align=center>" . $row['HighEstimate'] . "</td>";
        print "<td width=89 align=center>" . $row['StudyLocation'] . "</td>";
        print "<td width=139 align=center>" . $row['ReferenceID'] . "</td>";
        print "<td width=89 align=center>" . $row['Quality'] . "</td>";
        print "<td width=89 align=center>" . $row['Relevance'] . "</td>";
        print "<td width=61><a href='/sites/default/files/popupboxCD.php?SId=$vv' onClick='openpopup(this.href);return false;'>".$row['Info']."</a></td>";

        print "</tr>";
        print "<tr height='1'><td colspan='9' style='padding:0'><hr size='0.5' color='#CCCCCC' noshade='noshade' /></td></tr>";
    }
}

我想知道是否有任何方法可以将这两个表合并为一个表?

结果应如下所示:

Value1(两个查询通用,例如国家),Value2(来自第一个查询),Value2(来自第二个查询)

我在 mysql 中只有一个表,两个查询都显示结果。


对不起,我的问题让所有人感到困惑。我是 Lotus Notes 开发人员,而不是 PHP。所以我只是sql的新手用户。

在这里,我再次重复我的查询......

目前我从一个表的两个查询中得到了两个表的显示结果。我想将这些结果混合在一个表中吗?

那就是我的新表必须有三列。首先是两个表格中的公共值,第一个表格第二列的第二列和第二个表格第二列的第三列。新表中的第二列和第三列正在显示来自主 mysql 数据的同一列的数据。

希望这会变得更好,感谢所有建议。我会自己尝试,如果成功,我会在这里发布。

4

3 回答 3

4

与联合

( SELECT * FROM mtable WHERE SId= ? ) UNION (SELECT * FROM mtable WHERE SId= ?)

如果我正确理解了您的意思,请注意在这种情况下,对于这种简单的情况,您可以简单地执行以下操作:

SELECT * FROM mtable WHERE SId= ? OR SId = ?
于 2011-06-14T09:33:52.470 回答
2

很抱歉,我无法了解您正在寻找什么样的输出/结果。我无法解释您所说的“结果应该出现在单个表(php 页面)国家/地区、midestimate(来自第一个查询)、midestimate(来自第二个查询)国家、midestimate(来自第一个查询)、midestimate(来自 2nd查询)国家,midestimate(来自第一个查询),midestimate(来自第二个查询)等等“

无论如何,我认为您不需要运行第一个 SELECT 语句来检索 mtable.SId,然后运行其他多个 SELECT(在循环内)来从 mtable 中读取其他列。

我认为下面的单个查询应该可以工作,因为它合并了您的两个查询并返回一个结果集:

SELECT * FROM mtable 
WHERE (Pathogen='$pathogen1' && Topic='$topic1' && Indicator='$ind1' && IndicatorSubGroup='$subind1')
OR (Pathogen='$pathogen2' && Topic='$topic2' && Indicator='$ind2' && IndicatorSubGroup='$subind2');
于 2011-06-14T11:27:53.930 回答
1

您的两个查询:

  1. $num1 = mysql_query("SELECT SId FROM mtable WHERE Pathogen='$pathogen1' && Topic='$topic1' && Indicator='$ind1' && IndicatorSubGroup='$subind1' ");
  2. $num2 = mysql_query("SELECT SId FROM mtable WHERE Pathogen='$pathogen2' && Topic='$topic2' && Indicator='$ind2' && IndicatorSubGroup='$subind2' ");

它们只是WHERE部分不同。您可以通过将它们与 a 组合在一起来组合它们OR

mysql_query("
SELECT SId FROM mtable 
  WHERE
    (
      Pathogen='$pathogen1' && Topic='$topic1' 
      && Indicator='$ind1' && IndicatorSubGroup='$subind1'
    )
    OR
    (
      Pathogen='$pathogen2' && Topic='$topic2' 
      && Indicator='$ind2' && IndicatorSubGroup='$subind2'
    )
");

WHERE部分也是 Mysql 手册中记录的表达式:Expression Syntax。有多种方式可以表达您正在寻找的内容,上面的示例只是一种方式。

如果您需要有关该SELECT语句的更多一般信息,请参阅此处:SELECT Syntax


我首先认为这听起来像是 JOIN 的工作,但后来我看到您只有一个表mtable,因此根本不需要连接或联合,您可以在一个表上使用简单的选择查询来编写它:

SELECT * FROM mtable WHERE Pathogen='$pathogen2' AND Topic='$topic2' AND Indicator='$ind2' AND IndicatorSubGroup='$subind2'

反而。它应该返回您感兴趣的所有记录。

您的查询

mtable 包含 country、midestimate 等列。两个 select 语句使用不同的参数搜索同一个表。结果应该在一个表(php页面)国家,midestimate(来自第一个查询),midestimate(来自第二个查询)国家,midestimate(来自第一个查询),midestimate(来自第二个查询)国家,midestimate(来自第一个查询) , midestimate (来自第二个查询) 等等。感谢您的回复 – Gopipuli 42 分钟前

您要求将这两个查询合并为一个。这是可能的。你写你有两个选择语句。我尝试从您的问题中收集它们:

  1. $num1 = mysql_query("SELECT SId FROM mtable WHERE Pathogen='$pathogen1' && Topic='$topic1' && Indicator='$ind1' && IndicatorSubGroup='$subind1' ");
  2. `$num2 = mysql_query("SELECT SId FROM mtable WHERE Pathogen='$pathogen2' && Topic='$topic2' && Indicator='$ind2' && IndicatorSubGroup='$subind2' ");

这两个查询有一些部分`


如果您需要从两个或多个表中获取数据:

正如所写,首先对我来说听起来像是一个JOIN。使用联接,您可以根据两者之间的引用在结果中组合来自多个表的数据。需要在查询中制定参考。

更一般的解释是在WikipediaMysql 手册有语法

SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a

这将从表t1和表中t2为每个产品选择t2.aequals的所有字段t1.a

如果它,您可以在末尾添加一个 where 子句,自然您可以*只替换您想要返回的那些列名。由于您现在有两个表,如果您的表中有重复的列名,则需要在列前加上它的表前缀,否则 Mysql 将不知道该选择哪一个。

于 2011-06-14T10:04:23.980 回答