MySQL 如何将表名作为字段检索

发布于 2024-12-18 10:27:55 字数 862 浏览 3 评论 0原文

我不确定如何准确表达我的问题,但我会尽力而为。我正在尝试为驾驶学校建立数据库模型。我有一个“时间段”表,这样课程、测试和注册面试等活动都可以链接到工作人员的给定时间段。我提供的查询之一是能够查看工作人员的活动“时间表”。我已经构建了查询并且它正在工作,但是它连接了来自其他各个表的数据,我想查看这些表的名称以了解该时隙保留用于哪些活动。

ER 模型

我用来检查员工时间表的查询如下:

SELECT Timeslot.*
FROM Timeslot
LEFT JOIN Test
ON Timeslot.Timeslot_ID = Test.Timeslot
LEFT JOIN Interview
ON Timeslot.Timeslot_ID = Interview.Timeslot
LEFT JOIN Lesson
ON Timeslot.Timeslot_ID = Lesson.Timeslot
WHERE Timeslot.Date BETWEEN CURDATE() AND (CURDATE() + INTERVAL 7 DAY)
AND Timeslot.Staff = 1;

这可以工作并显示给定工作人员下周的所有注册时间段的列表。我想要的是一个进一步的列,它会显示它是什么类型的活动,例如“课程”、“面试”或“测试”。正如您所看到的,我当前将其存储为时隙表中的字段,这意味着每次插入时隙时都必须指定它。我会将数据库规范化为 3NF,并希望避免重复。有没有一种方法可以让我对此进行建模来获取表的名称,我考虑过使用 UNIONS 和许多其他东西,但可以使用一些帮助。

如果这看起来有点含糊,非常感谢和抱歉。 麦克风

Im not sure how to exactly word my issue but I will try my best. Im trying to model a database for a driving school. I have a "timeslot" table such that activities such as lessons, tests, and registration interviews can all be linked to a given timeslot for a staff member. One of the queries I am providing is to be able to view a "timetable" of events for a staff member. I have constructed the query and it is working, however it joins data from various other tables, and I would like to see the names of those tables to know what activity the timeslot is reserved for.

ER Model

The query I perform to check the staff timetable is the following:

SELECT Timeslot.*
FROM Timeslot
LEFT JOIN Test
ON Timeslot.Timeslot_ID = Test.Timeslot
LEFT JOIN Interview
ON Timeslot.Timeslot_ID = Interview.Timeslot
LEFT JOIN Lesson
ON Timeslot.Timeslot_ID = Lesson.Timeslot
WHERE Timeslot.Date BETWEEN CURDATE() AND (CURDATE() + INTERVAL 7 DAY)
AND Timeslot.Staff = 1;

This works and shows a list of all registered timeslots for a given staff member for the next week. What I would like is a further column which would show what type of activity it is, such as "Lesson", "Interview", or "Test". As you can see, I am currently storing this as a field in the timeslot table, which means that I have to specify this every time I insert a timeslot. I will be normalising the database to 3NF and want to avoid duplication. Is there a way I model this to get the name of the table, I considered using UNIONS and many other things but could use some help.

Many thanks and apologies if this seems a bit vague.
Mike

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

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

发布评论

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

评论(1

执笔绘流年 2024-12-25 10:27:55

如果您保留您所描述的模型,我的尝试将是使用 Case 语句,如下所示:

Select Timeslot.*,
    Case
        When Test.Timeslot Is Not Null Then 'Test'
        When Interview.Timeslot Is Not Null Then 'Interview'
        When Lesson.Timeslot Is Not Null Then 'Lesson'
    End As ActivityType
From ...

您的查询将在最后有一个您可以使用的“ActivityType”列。

My stab at it, if you're keeping the model you've described, would be with a Case statement, like so:

Select Timeslot.*,
    Case
        When Test.Timeslot Is Not Null Then 'Test'
        When Interview.Timeslot Is Not Null Then 'Interview'
        When Lesson.Timeslot Is Not Null Then 'Lesson'
    End As ActivityType
From ...

Your query would then have an "ActivityType" column at the very end that you could use.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文