如何在一行中获得两个select语句的结果? [英] How do I get the result of two select statement in a single row?

查看:111
本文介绍了如何在一行中获得两个select语句的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在php页面中使用两个选择语句,如下所示:

I am using two select statments in a php page as follows:

$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循环显示

at present the result is displayed using separate while loops as follows

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>";
    }
}

我想知道是否可以将这两个表合并为一个表?

I would like to know if there's any way to merge these two table into single table?

结果应如下所示:

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

Value1(common to both query., eg.country), Value2(from 1st query), Value2(from 2nd query)

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

I got only a single table in mysql which both query show results.

对不起,我的问题使所有人困惑.我是Lotus Notes Developer,而不是PHP.所以我只是sql的新手.

I am sorry my question confuses all. I am Lotus Notes Developer and not a PHP. So I am only a novice user in sql.

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

Here I repeat my query again...

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

At present I got two tables display result from two queries from a single table. I want to mix these result in a single table ?

那是我的新表必须有三列.首先是具有共同值的表,第二个表是第一表的第二列,第二个表是第二表的第二列.新表中的第二和第三列是从主要mysql数据的同一列中删除数据.

That is my new table must have three columns. First with common value from both tabels and second column from first table second column and third column from second table second column. Second and third column in new table is dispalying data from same column from the main mysql data.

希望所有建议都比这好得多.我会自己尝试,如果成功,我会在这里发布.

Hope this makes much better, thanx for all the suggestion. I will try this myself and if get succeed I will post it here.

推荐答案

使用UNION

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

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

If i understood correctly what you meant Also note in this case for this simple condition you can simply do:

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

这篇关于如何在一行中获得两个select语句的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆