简体   繁体   English

如何从包含两个引用同一列的外键的SQLite表中获取信息

[英]How to get at the information from a SQLite table containing two foreign keys referencing the same column

As part of a personal project, I have built a database which contains a table of text files (with their path and basename as columns), and another table, which indicates which files are duplicates of other files. 作为个人项目的一部分,我建立了一个数据库,其中包含一个文本文件表(其路径和基本名称作为列),以及另一个表,该表指示哪些文件是其他文件的副本。

Here's how the first table looks : 这是第一个表的外观:

files(
         filesid INTEGER PRIMARY KEY UNIQUE,
         filename TEXT,
         filesource TEXT,
         path TEXT UNIQUE)

And here's the second one : 这是第二个:

duplicatefiles(
            id INTEGER PRIMARY KEY UNIQUE,
            filesid INTEGER,
            duplicateof INTEGER,
            songid INTEGER,
            FOREIGN KEY(filesid) REFERENCES lyricsfiles(filesid),
            FOREIGN KEY(duplicateof) REFERENCES lyricsfiles(filesid),

This last table has two columns of foreign keys referencing ids in the files table, indicating that the two files are duplicate from each other. 最后一个表具有两列外键,这些外键引用了文件表中的ID,指示这两个文件彼此重复。

I did this to minimize information duplication but I now realise that I have no idea how to query this table to get what I actually want, which is the path to both files. 我这样做是为了最大程度地减少信息重复,但是现在我意识到我不知道如何查询该表以获取我真正想要的东西,这是两个文件的路径。 I have read about aliases, but I have been unable to find documentation on how to use them in this case. 我已经阅读了有关别名的信息,但是在这种情况下,我找不到有关如何使用别名的文档。

I'm using sqlite3 and python3. 我正在使用sqlite3和python3。

So my questions are : what's the correct way to query the database as it is to get at the information I need ? 所以我的问题是:查询数据库以获取所需信息的正确方法是什么? And what would be a better way to represent this information in a database ? 在数据库中表示此信息的更好方法是什么?

you need to join your table files 2 times. 您需要将表files加入2次。 To do so you need table aliases: 为此,您需要表别名:

SELECT
  ORI.filesid original_filesid
 ,ORI.filename original_filename
 ,ORI.filesource original_filesource
 ,DUP.filesid duplicate_filesid
 ,DUP.filename duplicate_filename
 ,DUP.filesource duplicate_filesource
FROM duplicatefiles DPF
JOIN files ORI
ON ORI.filesid = DPF.duplicateof
JOIN files DUP
ON DUP.filesid = DPF.filesid

you see that i joined files as the alias ORI indicating that this alias represents the original file (also see the join condition ORI.filesid = DPF.duplicateof ). 您会看到我以别名ORI的形式加入files ,这表明该别名表示原始文件(另请参见加入条件ORI.filesid = DPF.duplicateof )。 The duplicates are joined via the tables alias DUP. 通过表别名DUP连接重复项。 Using the alias you then select the field you want and you could also rename them for better understanding ORI.filesid is renamed to original_filesid . 然后使用别名选择所需的字段,还可以重命名它们以更好地理解ORI.filesid重命名为original_filesid

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQLite - 引用同一列的多个外键 - SQLite - Multiple Foreign Keys Referencing the Same Column 从另一个表中选择具有两个不同外键的同一列 - Select the same column with two different foreign keys from another table SQL 从带有 2 个外键的表中获取多个值(来自同一个表的同一列)到第一个表 - SQL get multiple values (from the same column from same table) from table with 2 foreign keys to the first table 引用同一个主键的两个外键 - Two foreign keys referencing the same primary key 如何在 SQLite 中获取有关外键的信息? - How do i get information about foreign keys in SQLite? 在一个表中使用同一列,在另一表上使用两个外键 - Using same column from one table with two foreign keys on other table SQLite:在子表中,如何从两个父表中指定两个外键 - SQLite: in a child table, how can I specify two foreign keys from two parent tables 如何在一个表中创建两个指向某个表中同一列的外键? - How to Create two Foreign Keys in one Table pointing to the same Column in some table? 如何使用SQL Server从引用同一表的两个键中联接一个表? - How can I join a table from two keys referencing same table with SQL Server? 两个外键引用另一个表的同一列? - Two Foreign Keys Refer same column of another table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM