从数据库表中查找包依赖关系
我在数据库中有两张表,一张列出了包,一张列出了依赖项:
packages
id | name
---------
0 | xorg
1 | gnome-session
2 | gnome-panel
3 | gnome-mixer-applet
4 | gnome-media
depends
package | depends
-----------------
1 | 0
2 | 1
3 | 2
4 | 2
显然,如果我想找出包所依赖的内容,我可以这样做:
SELECT *
FROM packages
INNER JOIN depends
ON packages.id = depends.package
WHERE packages.id = @somenumber
问题是这只给了我一级依赖项(4 取决于2,但它也取决于包 1 和 0)。有没有一种方法可以获取所有依赖项,而无需在循环中运行类似的 SELECT ?
我更喜欢它在 SQLite 中工作,但如果需要的话我会使用不同的数据库(只要它是免费的并且在 Linux 上可用)。
I have two tables in a database, one lists package and one lists dependencies:
packages
id | name
---------
0 | xorg
1 | gnome-session
2 | gnome-panel
3 | gnome-mixer-applet
4 | gnome-media
depends
package | depends
-----------------
1 | 0
2 | 1
3 | 2
4 | 2
Obviously, if I want to find out what a package depends on, I can do:
SELECT *
FROM packages
INNER JOIN depends
ON packages.id = depends.package
WHERE packages.id = @somenumber
The problem is that this only gives me one level of dependencies (4 depends on 2, but it also depends on packages 1 and 0). Is there a way to get all of the dependencies without just running similar SELECT
s in a loop?
I'd prefer that it works in SQLite, but I'll use a different database if I need to (as long as it's free and available on Linux).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
PostgreSQL 是唯一支持递归查询的开源 RDBMS 。例如,您可以运行以下命令:
SQLite 不支持递归查询,但还有其他几种解决方案可以用更简单的 SQL 管理分层数据。请参阅我的演示文稿使用 SQL 和 PHP 构建分层数据模型。
PostgreSQL is the only open-source RDBMS that supports recursive queries. For example, you could run this:
SQLite has no support for recursive queries, but there are several other solutions for managing hierarchical data in more plain SQL. See my presentation Models for Hierarchical Data with SQL and PHP.