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' ");
and
$num2 = mysql_query("SELECT SId FROM mtable WHERE Pathogen='$pathogen2' && Topic='$topic2' && Indicator='$ind2' && IndicatorSubGroup='$subind2' ");
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>";
}
}
and
$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?
The result should look like this:
Value1(common to both query., eg.country), Value2(from 1st query), Value2(from 2nd query)
I got only a single table in mysql which both query show results.
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 ?
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.
I'm sorry but I can't follow what kind of output / result you are looking for. I'm unable to interpret what you mean by "result should come in a single table(php page) country, midestimate (from 1st query), midestimate (from 2nd query) country, midestimate (from 1st query), midestimate (from 2nd query) country, midestimate (from 1st query), midestimate (from 2nd query) and so on"
Anyways, I think you don't need to run the first SELECT statement to retrieve mtable.SId and then other multiple SELECTs (within loop) to read other columns from mtable.
I think that the below single query should work as it merges your two queries and returns a single resultset:
with UNION
If i understood correctly what you meant Also note in this case for this simple condition you can simply do:
Your two queries:
$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' ");
They only differ in the
WHERE
part. You can combine those by grouping them together with aOR
:The
WHERE
part is an expression which is documented in the Mysql manual as well: Expression Syntax. There are multiple ways to express what you're looking for, the example above is only one way.If you need more general information about the
SELECT
statement, it is documented here: SELECT Syntax.I first thought this sounds like a job for a JOIN but then I saw you only have one table called
mtable
only, so a join or union is not necessary at all, you can write that with a simple select query over the one table:instead. It should return all the records you're interested in.
Your query
You asked about to combine those two queries into one. That's possible. You write you have two select statements. I try to gather them from your question:
$num1 = mysql_query("SELECT SId FROM mtable WHERE Pathogen='$pathogen1' && Topic='$topic1' && Indicator='$ind1' && IndicatorSubGroup='$subind1' ");
Those two queries have some part `
In case you need to take the data from two or more tables:
As written, first is sounded like a
JOIN
to me. With joins you can combine data from multiple tables in the result based on references between the two. The references need to be formulated in the query.A more general explanation is on Wikipedia, the Mysql manual has the syntax.
This selects all fields from both table
t1
and tablet2
for every product in whicht2.a
equalst1.a
.You can add a where clause at the end if it and naturally you can replace
*
with only those column names you want to return. As you now have two tables, you need to prefix the columns with it's table prefix if you have duplicate column names with the tables, otherwise Mysql would not know which one to choose.