选择计数(*) 或零

发布于 2024-09-11 03:30:28 字数 829 浏览 4 评论 0原文

由于太累,我找不到这个...假设您有以下表格:

Parent

  • PARENT_ID (LONG)

Child

  • CHILD_ID (LONG)
  • PARENT_ID (LONG, FK)
  • HAS_GRADUATED (BOOLEAN ) )

如果父母至少有一个已经毕业的孩子,我希望查询返回以下 true(1,对于 Oracle),以及 false(0,在 Oracle 的情况下)如果父母没有已毕业的孩子,或根本没有孩子

PARENT_ID................HAS_CHILDREN_WHO_GRADUATED

5.................................................1

3.................................................1

6.................................0

2.................................0

在上面,parent_id=5 的父级可能有 >=1 个已毕业的孩子。与parent_id=3 的父级相同。 Parent_id=6 的父母要么根本没有孩子,要么有孩子但都没有毕业。

对此的查询会是什么样的?

Tiredness prevents me from finding this one... Say you have the following tables:

Parent

  • PARENT_ID (LONG)

Child

  • CHILD_ID (LONG)
  • PARENT_ID (LONG, FK)
  • HAS_GRADUATED (BOOLEAN)

I want a query to return the following true (1, in the case of Oracle) if the parent has at least one child that has graduated, and false (0, in the case if Oracle) if the parent does not have a child that has graduated, or has no children at all:

PARENT_ID................HAS_CHILDREN_WHO_GRADUATED

5.................................1

3.................................1

6.................................0

2.................................0

In the above, parent with parent_id=5 may have >=1 children that have graduated. Same is parent with parent_id=3. Parent with parent_id=6 either has no children at all, or has children but none of them has graduated.

What would the query to this be like?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

昵称有卵用 2024-09-18 03:30:28

用途:

   SELECT DISTINCT
          p.parent_id,
          CASE WHEN c.parent_id IS NULL THEN 0 ELSE 1 END
     FROM PARENT p
LEFT JOIN CHILD c ON c.parent_id = p.parent_id
                 AND c.has_graduated = 1

您必须使用外连接才能查看子表中没有支持记录的父值。

Use:

   SELECT DISTINCT
          p.parent_id,
          CASE WHEN c.parent_id IS NULL THEN 0 ELSE 1 END
     FROM PARENT p
LEFT JOIN CHILD c ON c.parent_id = p.parent_id
                 AND c.has_graduated = 1

You have to use an outer join in order to see the parent values that don't have supporting records in the child table.

九八野马 2024-09-18 03:30:28

这会给你带来你所期望的吗?

SELECT 
    P.Parent_Id,
    CASE WHEN (SUM (CASE WHEN Has_Graduated = 1 then 1 else 0 END)) = 0 THEN 0 ELSE 1  as HAS_CHILDREN_WHO_GRADUATED
FROM Parent P
    LEFT JOIN Child C
        ON P.Parent_Id = C.Parent_Id
GROUP BY P.Parent_Id

Will this give you what you expect?

SELECT 
    P.Parent_Id,
    CASE WHEN (SUM (CASE WHEN Has_Graduated = 1 then 1 else 0 END)) = 0 THEN 0 ELSE 1  as HAS_CHILDREN_WHO_GRADUATED
FROM Parent P
    LEFT JOIN Child C
        ON P.Parent_Id = C.Parent_Id
GROUP BY P.Parent_Id
国产ˉ祖宗 2024-09-18 03:30:28

OMG Ponies 解决方案可能会表现得更好(这就是他得到我的 +1 的原因),但这是解决问题的另一种方法。

Select Parent_Id
    , Case
        When Exists( Select 1
                    From Child
                    Where Child.Parent_Id = Parent.Parent_Id
                        And Child.Has_Graduated = 1 ) Then 1
        Else 0
        End
From Parent

It is likely that OMG Ponies solution will perform better (which is why he got my +1), but this yet another way of solving the problem.

Select Parent_Id
    , Case
        When Exists( Select 1
                    From Child
                    Where Child.Parent_Id = Parent.Parent_Id
                        And Child.Has_Graduated = 1 ) Then 1
        Else 0
        End
From Parent
一笑百媚生 2024-09-18 03:30:28

首先,我认为您不能为此使用 LONG 列,因为 LONG 值不能在 WHERE 条件中使用。请注意,从 10g 开始这是正确的,因为这就是我使用的。

其次,我假设您的意思是您的子表应该有一个名为 PARENT_ID 的列,否则将无法链接这两个表。鉴于此,这个查询应该有效:

SELECT PARENT_ID, COUNT(1) FROM Child WHERE HAS_GRADUATED = 1 GROUP BY PARENT_ID

First of all I don't think you can use LONG columns for this since LONG values cannot be used in WHERE conditions. Note this is true as of 10g, since that's what I use.

Second I assume you mean that your child table should have a column called PARENT_ID otherwise there would be no way to link the two tables. Given that, this query ought to work:

SELECT PARENT_ID, COUNT(1) FROM Child WHERE HAS_GRADUATED = 1 GROUP BY PARENT_ID
蓦然回首 2024-09-18 03:30:28

以下是查询的形式,尽管 Oracle 的语法可能不正确:

SELECT
   Parent.PARENT_ID
  ,case count(Child.PARENT_ID) when 0 then 0 else 1 end HAS_CHILDREN_WHO_GRADUATED
 from Parent
  left outer join Child
   on Child.PARENT_ID = Parent.PARENT_ID
 where Child.HAS_GRADUATED = 1
 group by Parent.PARENT_ID

这将列出所有父项一次,并带有 HAS_CHILDREN_WHO_GRADUATED
根据需要设置为 1 或 0。

(编辑添加 where 子句)

Here's the form of the query, though the syntax for Oracle may be off:

SELECT
   Parent.PARENT_ID
  ,case count(Child.PARENT_ID) when 0 then 0 else 1 end HAS_CHILDREN_WHO_GRADUATED
 from Parent
  left outer join Child
   on Child.PARENT_ID = Parent.PARENT_ID
 where Child.HAS_GRADUATED = 1
 group by Parent.PARENT_ID

This will list all Parent items once, with HAS_CHILDREN_WHO_GRADUATED
set to 1 or 0 set as desired.

(Edited to add the where clause)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文