简体   繁体   English

如何计算MySql中另一个表中数据\\行的条目?

[英]How to count entries of a data\row in another table in MySql?

The database and entries are as in the figure below: Teacher table Course table数据库和条目如下图:教师表课程表

1) Show how many courses each teacher is teaching. 1) 显示每位教师教授的课程数量。 2) Give the name of the teacher who teaches more than two courses. 2) 给出教授两门以上课程的老师姓名。

Is it necessary to use join(s) or can we perform the actions by any other means?是否有必要使用 join(s) 或者我们可以通过任何其他方式执行操作吗?

Thank you in advance.先感谢您。 Any help is appriciated.任何帮助是appriciated。

A join is necessary, as you are trying to receive a result based on data from two tables.连接是必要的,因为您试图接收基于来自两个表的数据的结果。 To even know what courses a teacher is teaching, you must join the tables.甚至要知道老师正在教授哪些课程,您必须加入表格。

Imagine you were at a hackathon, and you met a friendly coder there.想象一下,你在参加黑客马拉松,在那里遇到了一位友好的程序员。 You remember one very distinct thing about him - we'll say he had a very distinct hair style that no one else at the hackathon had.你记得关于他的一件非常独特的事情——我们会说他有一个非常独特的发型,在黑客马拉松上没有其他人有过。 However, that's the only thing you can remember about him.然而,这是你唯一能记住的关于他的事情。 You ask your friend that was with you if he remembers your new found friend, describing him by his distinct hair style.你问和你在一起的朋友是否记得你的新朋友,用他独特的发型来描述他。 Lucky you, he remembers!幸运的是,他记得! Your friend then tells you all the other details about your new found hackathon companion (eg his name, where he was from, etc.).然后你的朋友告诉你关于你新发现的黑客马拉松伙伴的所有其他细节(例如他的名字,他来自哪里等)。 In this case, your friend is the join.在这种情况下,您的朋友是联接。 You only knew one distinct thing about the hackathon man (like a foreign key in a database), but it was distinct enough to get you the rest of the information about the man.你只知道一个关于黑客马拉松的人的独特的事情(比如数据库中的外键),但它足以让你了解关于这个人的其余信息。

Putting this in context of your situation, your tables will need to have a foreign key relationship.将此放在您的情况下,您的表将需要具有外键关系。 This will allow you to link the tables together so that you can see what courses each teacher is in charge of.这将允许您将表格链接在一起,以便您可以查看每位教师负责的课程。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM