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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器