我们如何编写mysql查询,其中父ID有一个孩子ID,下次孩子ID是一个父ID,我该怎么办? [英] how can we write mysql query where parent id have a child id and next time child id is a parent id how can i do?

查看:57
本文介绍了我们如何编写mysql查询,其中父ID有一个孩子ID,下次孩子ID是一个父ID,我该怎么办?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我该如何构建查询,其中父ID有五个孩子ID,之后该孩子ID是一个父ID并有五个孩子ID.如何在我们的query.table中仅调用一个.这个困惑.谢谢 问候 imadbaloch

how can i built the query where parent id have a five child id and after that child id is a parent id and have a five child id.how can i call in our query.table just only one.kindly help me about this confusion.thanks Regards imadbaloch

推荐答案

我不知道我是否正确,但我认为是这样的:您想进行一些递归查询,以请求一个有孩子的实体实体,它们也具有子实体(孙子实体).

I don't know if I got you right, but I think so: You'd like to have some recursive query to ask for an entity with some child entities, which on their part have child entities as well (grandchild entities).

不幸的是, MySQL在查询中不支持递归.

您必须使用多个查询手动进行操作,以询问子实体的子实体(孙子实体)及其子实体(孙子实体)等等.

You have to do it manually using multiple queries to ask for the child entities' child entities (grandchild entities) and their child entities (grandgrandchild entities) and so on.

更新:如果深度是固定的,例如,您对直接子实体和孙子实体(不再感兴趣)感兴趣,则可以执行两个联接,例如:

Update: If the depth is fixed, say, you are interested in the direct child entities and the grandchild entities (and no more), then you could perform two joins, e.g.:

SELECT e.id, e.name, ce.id, ce.name, gce.id, gce.name
FROM <table> AS e
LEFT JOIN <table> AS ce ON ce.parent_id = e.id
LEFT JOIN <table> AS gce ON gce.parent_id = ce.id

这篇关于我们如何编写mysql查询,其中父ID有一个孩子ID,下次孩子ID是一个父ID,我该怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆