dpicx06888 2011-01-31 12:50
浏览 120
已采纳

如何处理使用PHP从SQL查询返回的树结构?

This is a "theoretical" question.

I'm having trouble defining the question so please bear with me.

When you have several related tables in a database, for example a table that holds "users" and a table that holds "phones"

both "phones" and "users" have a column called "user_id"

select user_id,name,phone from users left outer join phones on phones.user_id = users.user_id;

the query will provide me with rows of all the users whether they have a phone or not.

If a user has several phones, his name will be returned in 2 rows as expected.

columns=>|user_id|name|phone|
row0 = > | 0 |fred|NULL|
row1 = > | 1 |paul|tlf1|
row2 = > | 1 |paul|tlf2|

the name "paul" in the case above is a necessary duplicate which in the RDMS's eye's is not a duplicate at all! It will then be handled by some server side scripting language, for example php.

How are these "necessary duplicates" actually handled in real websites or applications? as in, how are the row's "mapped" into some usable object model.

p.s. if you decide to post examples, post them for php,mysql,sqlite if possible.

edit:

Thank you for providing answers, each answer has interpreted the question differently and as such is different and correct in it's own way.

I have come to the conclusion that if round trips are expensive this will be the best way along with Jakob Nilsson-Ehle's solution, which was fitting for the theoretical question.

If round trips they are cheap, I will do separate selects for phones and users as 9000 suggests, if I need to show a single phone for every user, I will give a primary column to the phones and join it with the user select like Ollie Jones correctly suggests.

even though for real life applications I'm using 9000's answer, I think that for this unrealistic question Jakob Nilsson-Ehle's solution is most appropriate.

  • 写回答

4条回答 默认 最新

  • doty58493 2011-01-31 13:08
    关注

    The thing I would probably do in this case in PHP would be to use the userId in a PHP array and then use that to continuosly update the users

    A very simple example would be

    $result = mysql_query('select user_id,name,phone from users left outer join phones on phones.user_id = users.user_id;');
    $users = Array();
    while($row = mysql_fetch_assoc($result)) {
       $uid =$row['user_id'];
       if(!array_key_exists($uid, $users)) {
          $users[$uid] = Array('name' => $row['name'], 'phones' => Array());
       }
       $users[$uid]['phones'][] = $row['phone']; 
    }
    

    Of course, depending on your programming style and the complexity of the user data, you might define a User class or something and populate the data, but that is fundamentally how I would would do it.

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

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?