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

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您保留您所描述的模型,我的尝试将是使用 Case 语句,如下所示:
您的查询将在最后有一个您可以使用的“ActivityType”列。
My stab at it, if you're keeping the model you've described, would be with a Case statement, like so:
Your query would then have an "ActivityType" column at the very end that you could use.