小编DJ-*_*ing的帖子

使用 MySQL 在树中查找“叶子”

我试图识别树中的“叶子”,但我很困惑为什么我的查询没有给我我想要的东西。

问题是这样的:

在此输入图像描述

所以我的想法是,只要id不在p_id列中,那么它就应该是“Leaf”

select id, 'Leaf' as type
from Tree
where id not in (select distinct(p_id)
                 from Tree)
Run Code Online (Sandbox Code Playgroud)

但是,上面的查询没有给我返回任何结果。

该解决方案与我的几乎相同,只是它指定 p_id 不能为 NULL,然后它返回我想要的内容。

select id
from Tree
where id not in(select distinct(p_id) 
                from Tree 
                where p_id IS NOT NULL)
Run Code Online (Sandbox Code Playgroud)

我很困惑为什么添加 where 子句会产生影响?

mysql sql

2
推荐指数
1
解决办法
163
查看次数

返回 Python 中的类的实例

我正在尝试学习 Python 中的面向对象编程,但我对如何返回类的实例有点困惑。

我所拥有的代码的一些背景: Contact 类应该有一个必需的(完整)姓名、零个或多个电话号码以及零个或多个联系人的电子邮件地址。AddressBook 类采用联系人列表(定义如上),并且“add_contact”方法假设即使在创建联系人后也会将联系人添加到地址簿中。

class Contact:
    def __init__(self, name = None, addresses = False, email_addresses = False):
        self.name = name
        self.addresses = addresses
        self.email_addresses = email_addresses
class AddressBook:
    def __init__(self, contacts = []):
        self.contacts = contacts
    def add_contact(self,new_contact):
        self.contacts.append(new_contact)
    def contact_by_name(self, person):
        return Contact(name = person)
Run Code Online (Sandbox Code Playgroud)

所以我想要实现的是“contact_by_name”方法将采用名称(字符串)并返回具有给定名称的联系人 - 即它返回 Contact 的实例。

例子:

julian = Contact(name="Julian Berman")
book = AddressBook(contacts=[julian])
Run Code Online (Sandbox Code Playgroud)

我想要的是:

book.contact_by_name("Julian Berman") == julian
Run Code Online (Sandbox Code Playgroud)

我尽力给出“contact_by_name”方法的代码,但它一直给我错误,有人可以发送帮助吗!谢谢!

python class

0
推荐指数
1
解决办法
390
查看次数

标签 统计

class ×1

mysql ×1

python ×1

sql ×1