如何在一个表中显示mysql的两个查询的两个结果?
问题描述:
这是我的代码,结果是错误的。我想显示第一列中的第一个查询的结果和第二列中的第二个查询的结果,但是两个结果都放在一个列中(第一列)。像这样而不改变查询?如何在一个表中显示mysql的两个查询的两个结果?
/*
username username
username_a_1 username_b_1
username_a_1 username_b_4
username_a_2 username_b_1
username_a_2 username_b_4
username_a_3 username_b_5
username_a_4 username_b_2
username_a_4 username_b_3
username_a_5 username_b_1
username_a_5 username_b_4
*/
<html>
<head></head>
<table border="1" >
<tr>
<th>USERNAME</th>
<th>USERNAME</th>
</tr>
<?php
include'db_connect.php';
$query1='SELECT username FROM contacts_a ';
$query_run1=mysql_query($query1);
$query2="SELECT contacts_a.username,contacts_b.username FROM contacts_a LEFT JOIN contacts_b ON contacts_a.level=contacts_b.level";
$query_run2=mysql_query($query2);
while($query_array1=mysql_fetch_assoc($query_run1)){
foreach($query_array1 as $index => $names){
echo '<tr>
<td>'.(($names == NULL)? 'NULL': $names).'</td>
</tr>';
}//end of foreach
}//end of while
while($query_array2=mysql_fetch_assoc($query_run2)){
foreach($query_array2 as $index => $names){
echo '<tr>
<td>'.(($names == NULL)? 'NULL': $names).'</td>
</tr>';
}//end of foreach
}//end of while
?>
</table>
</html>
答
试试这个:
// Select the data, all together now. The difference is we'll give it a name
$query2="SELECT contacts_a.username as firstTableName,
contacts_b.username as secondTableName
FROM contacts_a
LEFT JOIN contacts_b
ON contacts_a.level = contacts_b.level";
// Execute the Query
$query_run2=mysql_query($query2);
// Loop over our results...
while($query_array1 = mysql_fetch_assoc($query_run1)) {
// We're going to use this again, give it a good name
$firstName = ($query_array1['firstTableName'] == NULL) ? 'NULL' :
$query_array1['firstTableName'];
$secondName = ($query_array1['secondTableName'] == NULL) ? 'NULL' :
$query_array1['secondTableName'];
// Put out a new table row
echo '<tr>';
// And our first TD...
echo '<td>' . firstName .'</td>';
echo '<td>' . secondName .'</td>';
echo '</tr>';
// End our loop
} // So Long and Thanks for All the Fish!
一些其他的想法:Don't use MySQL!你也可以摆脱现在第一次查询的,因为它是多余的。关于你的NULL检查,如果数据以空字符串的形式出现,会发生什么?通常我会检查null和空引号:“'或”“,以确保我能够找回我想要的内容。免责声明:我没有真正尝试这个,我使用PDO,所以我的MySQL语法用于取回和读取MySQL数组可能是不正确的!
+0
感谢您的回答,但我的大问题是如何填写两列表的同时,当我有2个查询,而你写的while循环没有做到这一点 – 2014-10-06 19:31:44
答
$qa1 = mysql_fetch_assoc($query_run1);
$qa2 = mysql_fetch_assoc($query_run2);
do {
echo '<tr>
<td>'.(current($qa1) == NULL ? 'NULL': current($qa1)).'</td>
<td>'.(current($qa2) == NULL ? 'NULL': current($qa2)).'</td>
</tr>';
} while(next($qa1) || next($qa2));
请不要使用'mysql_ *'函数](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php)。 *他们不再维护,并[已正式弃用](https://wiki.php.net/rfc/mysql_deprecation)*。学习[准备的语句](http://en.wikipedia.org/wiki/Prepared_statement),并使用[PDO](http://us1.php.net/pdo)或[MySQLi](http:// us1.php.net/mysqli)。 [本文](http://php.net/manual/en/mysqlinfo.api.choosing.php)将帮助你决定。 – 2014-10-06 18:12:15
您已在第二个查询中拥有所有需要的数据:contacts_a.username是第一个查询的用户名,contacts_b.username是第二个查询的用户名。只需循环第二组数据即可打印到您的表格。 – Mark 2014-10-06 18:14:09
...并向该查询中添加一个ORDER BY'子句,以获取您指定的序列中返回的行,而不是允许数据库以任意顺序返回行。 – spencer7593 2014-10-06 19:01:29