dongliugu8843 2013-05-20 00:13
浏览 50
已采纳

PHP Mysql左连接

<?php
$q = mysql_query("SELECT sub_cat.*, links.*
                 FROM links
                 LEFT JOIN sub_cat
                 ON links.p_id = sub_cat.id
                 WHERE sub_cat.p_id = '$id'
                 ORDER BY name ASC") or die (mysql_error());

while ($r = mysql_fetch_assoc($q))
{
    $links_name = $r['name'];
    $link_h3 = $links_name != '' ? '<h3>' . $links_name . '</h3>' : '';
    //print $link_h3;
    print '<pre>';
    print_r ($r);
}
?>

I have two tables with rows like:

sub_cat

  • id
  • name
  • p_id

links

  • id
  • links
  • p_id

In sub cat i have movie categories, like foreign language movies, national movies, uncategorised movies and so on. In links table i have concrete movie links and depending on sub category.

The only thing is that i do not want dublicate titles (sub_cat.name). result is:

Without Category www.moviesite.com

Without Category www.moviesite2.com

Without Category www.moviesite3.com

Foreign Movies www.moviesite1.bla

Foreign Movies www.moviesite2.bla

I want to be

Without Category www.moviesite.com

www.moviesite2.com

www.moviesite3.com

Foreign Movies www.moviesite1.bla

www.moviesite2.bla

and do not have any idea how to do this :(

any help appreciated.

  • 写回答

2条回答 默认 最新

  • dphphvs496524 2013-05-20 00:32
    关注

    To do the job, you have 2 solutions:

    The first solution is to process your data before showing it, in order to group all movies by category.

    You can do for example:

    $moviesByCategory = array();
    
    while ($r = mysql_fetch_assoc($q))
    {
        // Create the new sub array for the new category if necessary
        if (!isset($moviesByCategory[$r['name']]))
            $moviesByCategory[$r['name']] = array();
    
        // Add the movie in the category
        $moviesByCategory[$r['name']][] = $r['links'];
    }
    

    And then, you can now iterate on this new array like

    foreach($moviesByCategory as $category => $movies)
    {
        // Print the category name
        echo '<h1>' . $category . '</h1>';
    
        // Print all movies of the category
        foreach($movies as $movie)
            echo '<h3>' . $movie . '</h3>';
    }
    

    The second solution is to modify the SQL query to group directly all movies that have the same category. You just have to use a GROUP BY clause on sub_cat.id and then apply an agregate function on all other fields in the select.

    For performance aspect, the best solution is the SQL solution, but doing it with PHP will give you more flexibility for the presentation.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图