选择计数(*) 或零
由于太累,我找不到这个...假设您有以下表格:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
用途:
您必须使用外连接才能查看子表中没有支持记录的父值。
Use:
You have to use an outer join in order to see the parent values that don't have supporting records in the child table.
这会给你带来你所期望的吗?
Will this give you what you expect?
OMG Ponies 解决方案可能会表现得更好(这就是他得到我的 +1 的原因),但这是解决问题的另一种方法。
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.
首先,我认为您不能为此使用
LONG
列,因为LONG
值不能在WHERE
条件中使用。请注意,从 10g 开始这是正确的,因为这就是我使用的。其次,我假设您的意思是您的子表应该有一个名为 PARENT_ID 的列,否则将无法链接这两个表。鉴于此,这个查询应该有效:
First of all I don't think you can use
LONG
columns for this sinceLONG
values cannot be used inWHERE
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:
以下是查询的形式,尽管 Oracle 的语法可能不正确:
这将列出所有父项一次,并带有 HAS_CHILDREN_WHO_GRADUATED
根据需要设置为 1 或 0。
(编辑添加 where 子句)
Here's the form of the query, though the syntax for Oracle may be off:
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)