如何将重复条目计入MYSQL数据库并使用PHP显示结果

时间:2016-12-10 16:29:28

标签: php mysql

我在SQL数据库中有2个表,我想比较它们的列,然后当有多个重复的条目我想要计算它们并用PHP显示并创建一个只有重复的pageCatagorys的列表< / p>

我的两张桌子看起来像这样:

表:pageCatagorys

ID | catagory
1  | Home
2  | Test 1
3  | Other Services 

表:页面

ID | pageName | pageCatagory   | pageURL
1  | Test 1   | Test 1         | test-1
2  | Test 2   | Test 1         | test-2
3  | Test 3   | Home           | test-3
4  | Test 4   | Other Services | test-4

当我在phpMyAdmin中运行查询$pagecount = mysql_query("SELECT pageCatagory, COUNT(*) FROM pages GROUP BY pageCatagory HAVING COUNT(*) > 1");时,我得到以下结果表:

pageCatagory     | COUNT(*)
Test 1           |    2

但是它始终在我的代码中遵循else规则。

<?php
            $sql = mysql_query("SELECT catagory FROM pageCatagorys");

                while($row = mysql_fetch_assoc($sql)) { 
                    $pagecount = mysql_query("SELECT pageCatagory, COUNT(*) FROM pages GROUP BY pageCatagory HAVING COUNT(*) > 1");
                        if($pagecount > 1){
                        ?>
                        <li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"><?php echo $row["catagory"] ?><span class="caret"></span></a>
                        <ul class="dropdown-menu" role="menu">
                        <?php   $sql1 = mysql_query("SELECT pageName, pageURL FROM pageURL"); 
                            while($row1 = mysql_fetch_assoc($sql1)) { ?>
                            <li><a href="<?php echo $row["pageName"] ?>"><?php echo $row["pageName"] ?></a></li>
                            <li class="divider"></li>
                        <?php } ?>
                        </ul>
                <?php  }  else { ?> 
                <li><a href="#"><?php echo $row["catagory"] ?></a></li>
   <?php } } ?>

0 个答案:

没有答案