【问题标题】:How do I get the result of two select statement in a single row?如何在一行中获得两个 select 语句的结果?
【发布时间】:2011-09-14 13:23:54
【问题描述】:

我在一个 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(两个查询通用,例如country),Value2(来自第一个查询),Value2(来自第二个查询)

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


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

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

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

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

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

【问题讨论】:

标签: php mysql sql select statements


【解决方案1】:

与联合

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

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

SELECT * FROM mtable WHERE SId= ? OR SId = ?

【讨论】:

  • @hakre:是的,理想情况下,当您想从两个没有关系的表中检索行时,您最好使用UNION
  • 而在这种情况下,数据不会翻倍吗?
  • 是的,我试过了,但结果显示不是我想要的方式。它将显示如下国家,midestimate(来自第一个查询)国家,midestimate(来自第二个查询)国家,midestimate(来自第一个查询)国家,midestimate(来自第二个查询)等等......我的要求就像as follow country, midestimate (from 1st query), midestimate (from 2nd query) country, midestimate (from 1st query), midestimate (from 2nd query) .....thanx for the reply.
【解决方案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');

【讨论】:

  • +1;这是迄今为止正确回答问题的唯一答案。您只需要将 where 子句 OR 到单个 SELECT 语句中。
【解决方案3】:

您的两个查询:

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

它们仅在WHERE 部分有所不同。您可以通过将它们与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' &amp;&amp; Topic='$topic1' &amp;&amp; Indicator='$ind1' &amp;&amp; IndicatorSubGroup='$subind1' ");
  2. `$num2 = mysql_query("SELECT SId FROM mtable WHERE Pathogen='$pathogen2' && Topic='$topic2' && Indicator='$ind2' && IndicatorSubGroup='$subind2' ");

这两个查询有一部分 `


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

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

更一般的解释是在WikipediaMysql manual has the syntax

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

这会为t2.a 等于t1.a 的每个产品从表t1 和表t2 中选择所有字段。

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

【讨论】:

  • mtable 包含 country、midestimate 等列。两个 select 语句使用不同的参数搜索同一个表。结果应该在一个表(php页面)国家,midestimate(来自第一个查询),midestimate(来自第二个查询)国家,midestimate(来自第一个查询),midestimate(来自第二个查询)国家,midestimate(来自第一个查询) , midestimate (来自第二个查询) 等等。谢谢你的回复
  • Gopipuli,我实际上很难理解你的问题。我不知道你对 SQL 的了解程度,所以现在更新的答案可能是关于你试图找出的内容。可能这就是你要找的。​​span>
  • 我认为这接近我的要求 [linl]stackoverflow.com/questions/3613498/…
  • 可能是戈皮波利。只有你自己知道。
  • 您好 hakre,感谢您的支持。 “将多个查询数据放入单个 HTML 表(PHP、MySQL)”是我在这里试图表达的。无论如何还没有找到解决方案...
猜你喜欢
  • 2012-05-19
  • 1970-01-01
  • 2019-08-30
  • 2011-02-19
  • 1970-01-01
  • 2021-03-25
  • 1970-01-01
  • 2014-02-11
相关资源
最近更新 更多