为了账号安全,请及时绑定邮箱和手机立即绑定

MySQL单表查询子用户信息

MySQL单表查询子用户信息

id                                        parent_id                             name 2b8b06c0-58ba-11e7-8753-086266e296c7     null                                zx 30cf5287-58ba-11e7-8753-086266e296c7 2b8b06c0-58ba-11e7-8753-086266e296c7 zx@aa 32b58fa9-58ba-11e7-8753-086266e296c7 2b8b06c0-58ba-11e7-8753-086266e296c7 zx@bb 371a29a1-58ba-11e7-8753-086266e296c7 2b8b06c0-58ba-11e7-8753-086266e296c7 zx@cc以上为user表内容查询name为zx的子用户信息,怎么查效率较高?
查看完整描述

3 回答

已采纳
?
望远

TA贡献1017条经验 获得超1032个赞

select * from user where parent_id=(select id from user where name='zx' );

查看完整回答
反对 回复 2017-06-24
  • weixin_慕尼黑5583934
    weixin_慕尼黑5583934
    select u2.* from user u1, user u2 where u1.id = u2.parent_id and u1.name = 'zx'; 这样查可以吗?哪个效率更高?还有别的方法没?
  • 望远
    望远
    你这个更高,只对表操纵了一次,能尽量少操纵表就少操纵,单表查询的话就这样吧,没其他思路。
  • weixin_慕尼黑5583934
    weixin_慕尼黑5583934
    还有就是name值重复了怎么办?
?
望远

TA贡献1017条经验 获得超1032个赞

select * from user where name='zx';

查看完整回答
反对 回复 2017-06-24
  • 3 回答
  • 0 关注
  • 1858 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信