简体   繁体   English

将一个表中的多个外键引用到另一个表中的相同值并读取该值

[英]Referencing multiple foreign keys from one table to the same value in another table and reading the value

I am working on an app to create and manage background audio.我正在开发一个应用程序来创建和管理背景音频。 It is represented by scenes.它由场景表示。

Every scene has an intro, a background music and ambience.每个场景都有介绍、背景音乐和氛围。 I have to differ these audio types as in my app they are treated differently and can have own settings.我必须区分这些音频类型,因为在我的应用程序中,它们的处理方式不同并且可以有自己的设置。 Furthermore, while listing the scenes, I want to display every audio type in current scene by title.此外,在列出场景时,我想按标题显示当前场景中的每种音频类型。 In my currenct concept I have to join 3 tables and get a list of titles as result.在我当前的概念中,我必须加入 3 个表并得到一个标题列表作为结果。

Now I´ve been working on a concept, but I am not sure if this is the best way to do that.现在我一直在研究一个概念,但我不确定这是否是最好的方法。 Maybe could you help me to optimize it.也许你能帮我优化一下。

That is how the database looks like:这就是数据库的样子:

CREATE TABLE scene (
    id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, 
    title TEXT NOT NULL, 
    intro INTEGER,
    music INTEGER,
    ambience INTEGER,
    FOREIGN KEY (intro) REFERENCES audioInScene(id) ON DELETE CASCADE,
    FOREIGN KEY (music) REFERENCES audioInScene(id) ON DELETE CASCADE,
    FOREIGN KEY (ambience) REFERENCES audioInScene(id) ON DELETE CASCADE);

CREATE TABLE audioOpts (
    id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    volume INTEGER NOT NULL,
    repeat INTEGER NOT NULL);

CREATE TABLE audioFile (
    id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    title TEXT NOT NULL,
    path TEXT UNIQUE NOT NULL);

CREATE TABLE audioWithOpts (
    id INTEGER PRIMARY KEY,
    file INTEGER NOT NULL,
    opts INTEGER NOT NULL,
    FOREIGN KEY (file) REFERENCES audioFile (id),
    FOREIGN KEY (opts) REFERENCES audioOpts (id) ON DELETE CASCADE);

CREATE TABLE audioInScene(
    id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    inScene INTEGER NOT NULL,
    audio INTEGER NOT NULL,
    tag TEXT NOT NULL,
    FOREIGN KEY (inScene) REFERENCES scene (id) ON DELETE CASCADE,
    FOREIGN KEY (audio) REFERENCES audioWithOpts (id) ON DELETE CASCADE
);

And for example, this is a join statement that I have to execute to get the intro title, music title and ambience title:例如,这是一个连接语句,我必须执行它才能获得介绍标题、音乐标题和氛围标题:

SELECT scene.title, audioFile.title
FROM scene
INNER JOIN audioInScene ON scene.intro=audioInScene.audio OR scene.music=audioInScene.audio OR scene.ambience=audioInScene.audio
INNER JOIN audioWithOpts ON audioInScene.audio=audioWithOpts.id
INNER JOIN audioFile ON audioWithOpts.file=audioFile.id

And the output looks like:而 output 看起来像:

sceneTitle | introTitle
sceneTitle | musicTitle
sceneTitle | ambienceTitle

How could I create my concept to get output like this我怎么能创造我的概念来得到这样的 output

sceneTitle | introTitle | musicTitle | ambienceTitle

You can make use of STRING_AGG in SQL server to achieve this.您可以使用 SQL 服务器中的 STRING_AGG 来实现此目的。

select [sceneTitle], STRING_AGG(IntroTitle, ' | ') WITHIN GROUP (ORDER BY IntroTitle) AS IntroTitle
from dbo.table1 group  by [sceneTitle];

One option is to do 3 joins, you can use WITH to remove repetition.一种选择是进行 3 次连接,您可以使用 WITH 删除重复。

WITH audioMap AS (
  SELECT audioInScene.audio, audioFile.title
  FROM audioInScene
  INNER JOIN audioWithOpts ON audioInScene.audio=audioWithOpts.id
  INNER JOIN audioFile ON audioWithOpts.file=audioFile.id
)
SELECT scene.title, a1.title AS intro, a2.title AS music, a3.title AS ambience
FROM scene
INNER JOIN audioMap a1 ON scene.intro=a1.audio
INNER JOIN audioMap a2 ON scene.music=a2.audio
INNER JOIN audioMap a3 ON scene.ambience=a3.audio

Other option is to use your query and add PIVOT to convert rows to columns based on title.其他选项是使用您的查询并添加 PIVOT 以根据标题将行转换为列。

暂无
暂无

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

相关问题 如何使用Spring Boot中另一张表的外键从一张表中获取值? - How to get the value from one table using the foreign key from another table in Spring boot? 休眠-在同一张表上映射多个外键 - Hibernate - map multiple foreign keys on same table 休眠:来自同一表的两个外键 - Hibernate : Two Foreign Keys from the same Table Java/Mysql 从一个表中选择主键值并将它们作为外键插入到另一个表中 - Java/Mysql Selecting Primary Key values from one table and Insert them to another table as Foreign Keys sqlite如何在一张表中使用多个外键? - How to use multiple foreign keys in one table in sqlite? 如何从mysql中的两个表中获取值,并从一个表中获取不同值,并从另一个表中获取多个值? - how to get value from two table in mysql with from one table distinct and from other table multiple value? 使用来自相同表注释的两个外键来休眠 - Hibernate with two foreign keys from same table- annotation 在表中插入一行并引用其他表中的值 - Insert a row in a table with referencing to a value from other table 如何显示一个表中下拉框的第一个值和另一个表中相同下拉列表的其他值 - How can I display first value of drop down box from one table and other values of same drop down from another table 需要一个Java映射/表,其中包含多个键到一个值。 价值通常会改变 - Need a Java map/table with multiple keys to one value. Value is commonly altered
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM