合并两个sql查询以获得多个输出

时间:2016-01-10 10:22:13

标签: mysql

我有这两个问题:

$query = mysql_query("SELECT * from chapter where chapterID =1")or die('Query failed');
$query1 = mysql_query("SELECT * from page where chapterID =1")or die('Query failed');

我的php



<?php 
						$con=mysql_connect('localhost','root','') or die ('could not connect to db:'. mysql_error());
						mysql_select_db('learn') or die('No db found');
						$query = mysql_query("SELECT chapter.cDescription, page.pageName, page.pageIcon, chapter.cTitle FROM chapter LEFT JOIN page ON chapter.chapterID=page.chapterID GROUP BY page.pageID,page.chapterID")or die('Query failed');
						
						while ($result = mysql_fetch_array($query) )
						{
					
						echo "

                        <tr>
                            <td>
								<hr>
								<div class='chapter-container'>
								<div class='chapter-title' style='display: inline-block;float: left;'>
								<h3 style='margin-bottom: 22px;'>";
								
								echo $result['cTitle'];
							 
								echo "</h3>
								<div class='chapter-description'>";
								
								echo $result['cDescription']; 
								echo "
								</div>
								</div>
				
								<div class='chapter-contents' style='display: inline-block;float: left'>
								<div class='contents-box' style='margin: 15px 20px;'>
							
                                        <a href='networking_terms.html' style='display: block'>";
                                           
											echo "<span class='".$result['pageIcon']."' ></span>";
                                      
											echo "<span class='progress-title' data-toggle='tooltip' data-placement='right' title='Get familiar with the terms!'>";
											
											echo $result['pageName']; 
											
											echo "</span>
                                        </a>";
						}
				
					?>
&#13;
&#13;
&#13;

我想在一个查询中合并这两个。 我怎样才能合并这两个查询? 注意:1章有很多页面

我想要实现的目标 enter image description here

我实际得到的是什么 enter image description here

2 个答案:

答案 0 :(得分:1)

<强>更新:

试试这个:

// make an array
$yourData
while ($result = mysql_fetch_array($query) )
{

$yourData[$result['chapterId']][] = $result;

}

比在HTML中打印它,想法是:

foreach( $yourData as $key => $value ){

  //your stuff
  echo $value[0]['chapterName']."<br>". $value[0]['chapDesc']; // print chaprer name
  foreach( $value as $final ){

  //your html stuff
  echo $final["pageName"]; // print page name

  }
}

更新3(使用原始HTML):

$myArr = array();
foreach ($array as $key => $value) {
    $myArr[$value['cTitle']][] = $value;
}

foreach ($myArr as $key2 => $value) {
echo "

<tr>
<td>
<hr>
<div class='chapter-container'>
<div class='chapter-title' style='display: inline-block;float: left;'>
<h3 style='margin-bottom: 22px;'>";

echo $value[0]['cTitle'];

echo "</h3>
<div class='chapter-description'>";

echo $value[0]['cDescription']; 
echo "
</div>
</div>

";

foreach ($value as $key => $value2) {
echo "
<div class='chapter-contents' style='display: inline-block;float: left'>
<div class='contents-box' style='margin: 15px 20px;'>

<a href='networking_terms.html' style='display: block'>";

echo "<span class='".$value2['pageIcon']."' ></span>";

echo "<span class='progress-title' data-toggle='tooltip' data-placement='right' title='Get familiar with the terms!'>";

echo $value2['pageName']; 

echo "</span>
</a>";
}
}
?>

旁注:

我建议您使用mysqli_ *或PDO而不是mysql_ *因为它已被弃用且在PHP 7中不可用

答案 1 :(得分:0)

听起来你想要一个包含两个查询结果的数据集?为此,您应该使用UNION:

$query = mysql_query("SELECT * FROM Chapter WHERE ChapterID = 1
    UNION
    SELECT * FROM Page WHERE ChapterID = 1") or die('Query failed');

为此,表的列必须在数字和类型上匹配。如果他们不这样做,您必须在两个select语句中选择特定列,并且那些列应匹配。