[英]extract columns from different tables and display next to each other
我想從兩個彼此相鄰的表中提取列。
我有如下的PHP代碼:
<table>
<tr>
<th>Date</th>
<th>Performance A </th>
<th>Performance B </th>
</tr>
$sql = "select date, Aperformance from table1 group by date";
$sql1 = "select date, Bperformance from table2 group by date";
$result = mssql_query($sql);
while ($column = mssql_fetch_array($result))
{
echo "<tr><td>".$column[0]."</td>";
echo "<td>".$column[1]."</td></tr>";
}
這將產生如下輸出:
Date | Performance A | Performance B
01/05/12 | 40% |
02/05/12 | 30% |
03/05/12 | 25% |
現在,我想填充查詢$ sql1的第三列。 我不要$ sql1中的date列,Bperformance的第二列需要放在這里。 我該怎么做?
您可以簡單地使用聯接,如果其中一個表中沒有日期匹配,則使用完全外部聯接可以檢索行。
SELECT A.Date , A.APerformance AS [Performance A] , B.BPerformance AS [Performance B]
FROM (
SELECT Date
,APerformance
FROM Table1
GROUP BY Date
) A
FULL OUTER JOIN (
SELECT Date
,BPerformance
FROM Table2
GROUP BY Date
) B ON A.Date= B.Date
假設兩個查詢返回相同數量的行,則可以簡單地執行以下操作:
$result1 = mssql_query($sql);
$result2 = mssql_query($sql2);
while($column1 = mssql_fetch_array($result1)) {
$column2 = mssql_fetch_array($result2);
echo "<tr><td>$column1[0]</td><td>$column2[0]</td></tr>";
}
如果查詢返回不同大小的結果集,那么您將需要做一些額外的工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.