从1中的2个查询合并2个条件数组

时间:2015-11-03 21:05:42

标签: php mysql sql arrays merge

我只是一个脚本编码的业余爱好者。我需要这里的人帮忙.. 我在这里遇到麻烦我试图从我的代码中显示2条件合并,请看这里...



<ul class="nav navbar-nav">
   <?php               
   $main=mysql_query("SELECT * FROM mainmenu WHERE aktif='Y'");
   while($r=mysql_fetch_array($main)){
   $t=$r[''];
   $tm="<a href='$r[link]' class='dropdown-toggle' data-toggle='dropdown' role='button' aria-expanded='false'>$r[nama_menu]<span class='caret'></span></a>";
   $th="<a href='$r[link]'>$r[nama_menu]</a>";
   if ($t!= ""){
   $tombol=$th;
   }else{
   $tombol=$tm;
   } 
   echo "<li class='dropdown'>$tombol
         <ul class='dropdown-menu' role='menu'>";
    	 $sub=mysql_query("SELECT * FROM submenu, mainmenu  
         WHERE submenu.id_main=mainmenu.id_main
         AND submenu.id_main=$r[id_main]");
    	 while($w=mysql_fetch_array($sub)){
   echo " <li><a href='$w[link_sub]'>$w[nama_sub]</a></li>";
    	 }
   echo "</ul></li>";}
   ?>
</ul>
&#13;
&#13;
&#13;

我把它分开了,尝试在任何条件下展示它们,这更相关,但我不能合并它,我不知道我做了什么,请看这个请

这是第一个条件 - &gt;

&#13;
&#13;
<ul class="nav navbar-nav">
<?php               
    $main=mysql_query("SELECT  DISTINCT    a.* FROM mainmenu a
    INNER JOIN submenu b ON a.id_main = b.id_main AND a.aktif = 'Y'");
    while($r=mysql_fetch_array($main)){
    echo "<li class='dropdown'><a href='$r[link]' class='dropdown-toggle' data-toggle='dropdown' role='button' aria-expanded='false'>$r[nama_menu]<span class='caret'></span></a><ul class='dropdown-menu' role='menu'>";
    $sub=mysql_query("SELECT * FROM submenu, mainmenu  
    WHERE submenu.id_main=mainmenu.id_main 
    AND submenu.id_main=$r[id_main]");
    while($w=mysql_fetch_array($sub)){
    echo " <li><a href='$w[link_sub]'>$w[nama_sub]</a></li>";
    }
   echo "</ul></li>";} 
?>
</ul>
&#13;
&#13;
&#13;

而这是第二个条件 - &gt;

&#13;
&#13;
<ul class="nav navbar-nav">
<?php      
  $menu=mysql_query("SELECT  DISTINCT    a.* FROM mainmenu a
  LEFT OUTER JOIN submenu b ON a.id_main = b.id_main WHERE b.id_main is null AND a.aktif = 'Y'");
  while($s=mysql_fetch_array($menu)){
  echo "<li class='dropdown'><a href='$s[link]'>$s[nama_menu]</a></li>";} ?>
</ul>
&#13;
&#13;
&#13;

将INNER JOIN与LEFT OUTER JOIN合并时我想要的是:

&#13;
&#13;
if (bla, bla, bla){
echo "Show INNER JOIN";
}else{
echo "Show LEFT OUTER JOIN";
} 
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

通常,当两个查询使用相同的表时,使用OR加入两组条件就很简单了。但是,在这种情况下,一个查询具有JOIN,另一个查询具有LEFT OUTER JOIN

我认为你可以像这样使用UNION

SELECT  DISTINCT    a.* 
FROM mainmenu a
INNER JOIN submenu b ON a.id_main = b.id_main AND a.aktif = 'Y'
UNION 
SELECT  DISTINCT    a.* 
FROM mainmenu a
LEFT OUTER JOIN submenu b ON a.id_main = b.id_main 
WHERE b.id_main is null AND a.aktif = 'Y'

你或许可以将其改为:

SELECT  DISTINCT    a.* 
FROM mainmenu a
LEFT OUTER JOIN submenu b ON a.id_main = b.id_main OR b.id_main is null
WHERE a.aktif = 'Y'

没有样本数据&amp;结果我无法测试并确认。

答案 1 :(得分:0)

也许有人需要这样的东西,当有些菜单有子菜单时,他会从菜单中做出不同的外观.... 最后我找到了类似这样的解决方案。

这是剧本:

<ul class="nav navbar-nav">
            <?php               
        $main=mysql_query("SELECT * FROM mainmenu WHERE aktif='Y'");
		while($r=mysql_fetch_array($main)){
		$main2=mysql_query("SELECT  id_main FROM submenu WHERE submenu.id_main=$r[id_main] ");
		($s=mysql_fetch_array($main2));
		if ($r['id_main'] == $s['id_main'] ){
      $tombol="<a href='$r[link]' class='dropdown-toggle' data-toggle='dropdown' role='button' aria-expanded='false'>$r[nama_menu]<span class='caret'></span></a>";
      }else{
      $tombol="<a href='$r[link]'>$r[nama_menu]</a>";
      } 
	         echo "<li class='dropdown'>$tombol
                    <ul class='dropdown-menu' role='menu'>";
	         $sub=mysql_query("SELECT * FROM submenu, mainmenu  
                            WHERE submenu.id_main=mainmenu.id_main 
                            AND submenu.id_main=$r[id_main]");
	         while($w=mysql_fetch_array($sub)){
              echo " <li><a href='$w[link_sub]'>$w[nama_sub]</a></li>";
	         }
	       echo "</ul>
            </li>";} ?>
</ul>