左JOIN查询返回仅在表2中存在的结果

发布于 2025-01-23 04:45:39 字数 5012 浏览 0 评论 0原文

我想改进现有的(PHP/MySQL)代码库,该代码库概述了合唱团的出勤率(X Axeis:DateS,Y Axis:a轴:学生)。 数据分布在各种表中(如您所见,非常完美的命名逻辑)。查询非常重,并且要为每个“合唱”重复,因此我正在尝试构建一种视图来优化后端代码。

所涉及的表格:

  • Chorale_dates =本季
  • Chorale_inscriptions的排练日期=合唱者的注册
  • chorale_presences =记录的缺席(合唱者出勤)
  • chorale_id = shorale_id =合唱者的个人详细信息(电子邮件,名称,姓名)
┌───────────────────────┐      ┌──────────────────────────────────┐  ┌───────────────────────┐
│ chorale_dates         │      │chorale_presences                 │  │ chorale_inscription   │
├───────────────────────┤      ├──────────────────────────────────┤  ├───────────────────────┤
│                       │      │                                  │  │                       │
│ chorale_date_id       │      │chorale_presences_id            ┌─┼──┤►chorale_inscription_id│
│              ▲        │      │                                │ │  │                       │
│              │        │      │                                │ │  │                       │
│              │        │      │                                │ │  │                       │
│              └────────┼──────┤chorale_presences_date_id       │ │  │ chorale_id_id         │
│                       │      │                                │ │  │ │                     │
│                       │      │chorale_presences_inscription_id│ │  │ │                     │
│                       │      │                                  │  │ │                     │
│                       │      │                                  │  │ │                     │
└───────────────────────┘      └──────────────────────────────────┘  └─┼─────────────────────┘
                                                                       │
                                                                       │            ┌───────────────────────┐
                                                                       │            │ chorale_id            │
                                                                       │            ├───────────────────────┤
                                                                       │            │                       │
                                                                       └────────────► chorale_id_id         │
                                                                                    │                       │
                                                                                    │                       │
                                                                                    │                       │
                                                                                    │                       │
                                                                                    │                       │
                                                                                    │                       │
                                                                                    │                       │
                                                                                    └───────────────────────┘

是生成视图的陈述:


CREATE VIEW `attendances` AS SELECT
    `d`.`chorale_date_id` AS `date_id`,
    `d`.`chorale_date` AS `date`,
    `d`.`chorale_date_saison` AS `season_id`,
    `d`.`chorale_date_type` AS `date_type`,
    `d`.`chorale_date_chorale_id` AS `chorale_id`,
    `d`.`chorale_date_etat` AS `date_etat`,
    `p`.`chorale_presences_id` AS `absence_id`,
    `p`.`chorale_presences_date_id` AS `chorale_presences_date_id`,
    `p`.`chorale_presences_inscription_id` AS `chorale_presences_inscription_id`,
    `i`.`chorale_inscription_id` AS `chorale_inscription_id`,
    `p`.`chorale_presences_etat` AS `attendance`,
    `i`.`chorale_inscription_pupitre` AS `chorale_inscription_pupitre`,
    `p`.`chorale_presences_validation` AS `chorale_presences_validation`,
    CONCAT( `c`.`chorale_id_nom`, ', ', `c`.`chorale_id_prenom`) AS `fullname`
FROM
    (
        (
            (
                `chorale_dates` `d`
            LEFT JOIN `chorales_presences` `p` ON
                (
                    (
                        `p`.`chorale_presences_date_id` = `d`.`chorale_date_id`
                    )
                )
            )
        LEFT JOIN `chorale_inscription` `i` ON
            (
                (
                    `p`.`chorale_presences_inscription_id` = `i`.`chorale_inscription_id`
                )
            )
        )
    LEFT JOIN `chorale_id` `c` ON
        (
            (
                `c`.`chorale_id_id` = `i`.`chorale_id_id`
            )
        )
    )
ORDER BY d.season_id ASC, `d`.`chorale_date` ASC ;

因为这是一个左联接,所以我希望所有注册的合唱者都有所有季节的日期,如果Chorale_presence表中没有记录的缺席,则为空的“出勤”值,或者如果有匹配记录,则列值在该表中。无论是否有Chorale_presence中的条目,所有合唱者都应有37个条目。

但是,此视图返回的内容仅包含具有日期和缺勤的行。因此,根据合唱者的不同,我得到了不同的条目。

我在做什么错?

I would like to improve an existing (php/mysql) codebase that presents an overview of choir attendances (X axis: dates, Y axis: students).
The data is spread across various tables (and a very perfectible naming logic, as you will see). The query is quite heavy and to be repeated for each "chorist" so I am trying to construct a VIEW to optimize the backend code.

The involved tables:

  • chorale_dates = the rehearsal dates for the season
  • chorale_inscriptions = the chorists' registrations
  • chorale_presences = the recorded absences (chorists attendances)
  • chorale_id = the chorists' personal details (email, name, ...)
┌───────────────────────┐      ┌──────────────────────────────────┐  ┌───────────────────────┐
│ chorale_dates         │      │chorale_presences                 │  │ chorale_inscription   │
├───────────────────────┤      ├──────────────────────────────────┤  ├───────────────────────┤
│                       │      │                                  │  │                       │
│ chorale_date_id       │      │chorale_presences_id            ┌─┼──┤►chorale_inscription_id│
│              ▲        │      │                                │ │  │                       │
│              │        │      │                                │ │  │                       │
│              │        │      │                                │ │  │                       │
│              └────────┼──────┤chorale_presences_date_id       │ │  │ chorale_id_id         │
│                       │      │                                │ │  │ │                     │
│                       │      │chorale_presences_inscription_id│ │  │ │                     │
│                       │      │                                  │  │ │                     │
│                       │      │                                  │  │ │                     │
└───────────────────────┘      └──────────────────────────────────┘  └─┼─────────────────────┘
                                                                       │
                                                                       │            ┌───────────────────────┐
                                                                       │            │ chorale_id            │
                                                                       │            ├───────────────────────┤
                                                                       │            │                       │
                                                                       └────────────► chorale_id_id         │
                                                                                    │                       │
                                                                                    │                       │
                                                                                    │                       │
                                                                                    │                       │
                                                                                    │                       │
                                                                                    │                       │
                                                                                    │                       │
                                                                                    └───────────────────────┘

Here is the statement generating the view:


CREATE VIEW `attendances` AS SELECT
    `d`.`chorale_date_id` AS `date_id`,
    `d`.`chorale_date` AS `date`,
    `d`.`chorale_date_saison` AS `season_id`,
    `d`.`chorale_date_type` AS `date_type`,
    `d`.`chorale_date_chorale_id` AS `chorale_id`,
    `d`.`chorale_date_etat` AS `date_etat`,
    `p`.`chorale_presences_id` AS `absence_id`,
    `p`.`chorale_presences_date_id` AS `chorale_presences_date_id`,
    `p`.`chorale_presences_inscription_id` AS `chorale_presences_inscription_id`,
    `i`.`chorale_inscription_id` AS `chorale_inscription_id`,
    `p`.`chorale_presences_etat` AS `attendance`,
    `i`.`chorale_inscription_pupitre` AS `chorale_inscription_pupitre`,
    `p`.`chorale_presences_validation` AS `chorale_presences_validation`,
    CONCAT( `c`.`chorale_id_nom`, ', ', `c`.`chorale_id_prenom`) AS `fullname`
FROM
    (
        (
            (
                `chorale_dates` `d`
            LEFT JOIN `chorales_presences` `p` ON
                (
                    (
                        `p`.`chorale_presences_date_id` = `d`.`chorale_date_id`
                    )
                )
            )
        LEFT JOIN `chorale_inscription` `i` ON
            (
                (
                    `p`.`chorale_presences_inscription_id` = `i`.`chorale_inscription_id`
                )
            )
        )
    LEFT JOIN `chorale_id` `c` ON
        (
            (
                `c`.`chorale_id_id` = `i`.`chorale_id_id`
            )
        )
    )
ORDER BY d.season_id ASC, `d`.`chorale_date` ASC ;

Because it is a LEFT JOIN, I am expecting to have all season dates for all registered chorists, and either an empty "attendance" value if there is no recorded absence in the chorale_presence table, or the column value if there is a matching record in that table. All chorists should have 37 entries, whether there is an entry in chorale_presence or not.

Yet, what this view returns only contains rows having date and an absence recorded. So depending on the chorist, I get varying amount of entries.

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里梦着梦中梦 2025-01-30 04:45:40

我希望所有注册的合唱者都有整个季节

这是crosschorale_dates之间加入的定义。您存储合唱者的注册的位置:

CREATE VIEW attendances AS 
SELECT
  d.chorale_date_id AS date_id,
  d.chorale_date AS date,
  d.chorale_date_saison AS season_id,
  d.chorale_date_type AS date_type,
  d.chorale_date_chorale_id AS chorale_id,
  d.chorale_date_etat AS date_etat,
  p.chorale_presences_id AS absence_id,
  p.chorale_presences_date_id AS chorale_presences_date_id,
  p.chorale_presences_inscription_id AS chorale_presences_inscription_id,
  i.chorale_inscription_id AS chorale_inscription_id,
  p.chorale_presences_etat AS attendance,
  i.chorale_inscription_pupitre AS chorale_inscription_pupitre,
  p.chorale_presences_validation AS chorale_presences_validation,
  CONCAT(c.chorale_id_nom, ', ', c.chorale_id_prenom) AS fullname
FROM chorale_dates d CROSS JOIN chorale_inscription i
LEFT JOIN chorales_presences p ON p.chorale_presences_date_id = d.chorale_date_id AND p.chorale_presences_inscription_id = i.chorale_inscription_id
LEFT JOIN chorale_id c ON c.chorale_id_id = i.chorale_id_id
ORDER BY d.season_id ASC, d.chorale_date ASC;

I am expecting to have all season dates for all registered chorists

This is the definition of a CROSS join between chorale_dates where you store the dates and chorale_inscriptio where you store the chorists' registrations:

CREATE VIEW attendances AS 
SELECT
  d.chorale_date_id AS date_id,
  d.chorale_date AS date,
  d.chorale_date_saison AS season_id,
  d.chorale_date_type AS date_type,
  d.chorale_date_chorale_id AS chorale_id,
  d.chorale_date_etat AS date_etat,
  p.chorale_presences_id AS absence_id,
  p.chorale_presences_date_id AS chorale_presences_date_id,
  p.chorale_presences_inscription_id AS chorale_presences_inscription_id,
  i.chorale_inscription_id AS chorale_inscription_id,
  p.chorale_presences_etat AS attendance,
  i.chorale_inscription_pupitre AS chorale_inscription_pupitre,
  p.chorale_presences_validation AS chorale_presences_validation,
  CONCAT(c.chorale_id_nom, ', ', c.chorale_id_prenom) AS fullname
FROM chorale_dates d CROSS JOIN chorale_inscription i
LEFT JOIN chorales_presences p ON p.chorale_presences_date_id = d.chorale_date_id AND p.chorale_presences_inscription_id = i.chorale_inscription_id
LEFT JOIN chorale_id c ON c.chorale_id_id = i.chorale_id_id
ORDER BY d.season_id ASC, d.chorale_date ASC;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文