如何从多个表中选择数据?

发布于 2024-12-04 10:59:42 字数 616 浏览 1 评论 0原文

我有 4 个表,我对选择数据感到非常困惑。

的 4 个表中有此字段

t1 -> student_id
  given_name
  user_name

t2 -> school_year
  sem
  class
  student_id
  main_id

t3 -> subject_id
  main_id

t4 -> subject_id
  subject_name

我需要选择 (given_name、subject_name、class 和 main_id)。如何?

我尝试这样

SELECT t2.school_year, t2.rp_main_sem, t2.rp_main_class,  t4.name, t4.subject_id, t1.given_name, t1.user_name FROM t1, t2, t3, t4 WHERE t2.school_year = 2011 AND t2.sem = 1 AND t2.class = 'ES3A' AND t3.subject_id = t4.subject_id AND t1.student_id = t2.rp_main_student_id

I have 4 tables, i am very confused to select the data.

I have this fields in my 4 tables

t1 -> student_id
  given_name
  user_name

t2 -> school_year
  sem
  class
  student_id
  main_id

t3 -> subject_id
  main_id

t4 -> subject_id
  subject_name

I need to select (given_name, subject_name, class & main_id). How?

I try like this

SELECT t2.school_year, t2.rp_main_sem, t2.rp_main_class,  t4.name, t4.subject_id, t1.given_name, t1.user_name FROM t1, t2, t3, t4 WHERE t2.school_year = 2011 AND t2.sem = 1 AND t2.class = 'ES3A' AND t3.subject_id = t4.subject_id AND t1.student_id = t2.rp_main_student_id

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

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

发布评论

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

评论(6

枕头说它不想醒 2024-12-11 10:59:43
   SELECT
      t1.given_name,
      t3.subject_name,
      t2.class,
      t2.main_id 
   FROM
      t1 
   JOIN
      t2 
         ON t1.student_id = t2.student_id 
   JOIN
      t3 
         ON t2.main_id = t3.main_id 
   JOIN
      t4 
         ON t3.subject_id = t4.subject_ID

这应该可以做到,但请告诉我们您需要什么形式的数据。您想获取每个学生的课程吗?或每个班级的学生人数?

   SELECT
      t1.given_name,
      t3.subject_name,
      t2.class,
      t2.main_id 
   FROM
      t1 
   JOIN
      t2 
         ON t1.student_id = t2.student_id 
   JOIN
      t3 
         ON t2.main_id = t3.main_id 
   JOIN
      t4 
         ON t3.subject_id = t4.subject_ID

this should do it but please tell us in what form you need the data. do you want to get the classes per student? or students per class?

您将需要加入表格。每个表都应该有一个主键(或唯一键)以及对其他表的引用(所谓的外键)。

SELECT given_name , t4.subject_name, t2.class, t3.main_id
FROM t1 
JOIN t2 
ON t1.student_id = t2.student_id
JOIN t3
ON t2.main_id = t3.main_id
JOIN t4
ON t2.subject_id = t3.subject_id
WHERE t2.school_year = "2011""  and t2.sem = 1 --added where because of your comment

因此,我们假设

  • 学生表 t1 具有 PK student_id
  • 科目表 t4 具有 PK subject_id
  • t2 是一个关联表,用于链接学生(使用他们的
    student_id)到具有 main_id 的类。
  • t3 是一个将主题链接到类的关联表。

You will need to JOIN the tables. Each table should have a PRIMARY (or UNIQUE) key, and references (so called FOREIGN KEYs) to the other tables.

SELECT given_name , t4.subject_name, t2.class, t3.main_id
FROM t1 
JOIN t2 
ON t1.student_id = t2.student_id
JOIN t3
ON t2.main_id = t3.main_id
JOIN t4
ON t2.subject_id = t3.subject_id
WHERE t2.school_year = "2011""  and t2.sem = 1 --added where because of your comment

So we assume that

  • the students' table t1 has PK student_id
  • the subjects' table t4 has PK subject_id
  • t2 is a associative table that links students (using their
    student_id) to classes, which have a main_id.
  • t3 is an associative table that links subjects to classes.
Hello爱情风 2024-12-11 10:59:43
SELECT table_name1.given_name,table_name4.subject_name,
table_name2.class,table_name2.main_id
FROM table_name1
INNER JOIN table_name2
ON table_name1.studen_id=table_name2.studen_id
INNER JOIN table_name3
ON table_name2.main_id=table_name3.main_id
INNER JOIN table_name4
ON table_name3.subject_id=table_name4.subject_id
SELECT table_name1.given_name,table_name4.subject_name,
table_name2.class,table_name2.main_id
FROM table_name1
INNER JOIN table_name2
ON table_name1.studen_id=table_name2.studen_id
INNER JOIN table_name3
ON table_name2.main_id=table_name3.main_id
INNER JOIN table_name4
ON table_name3.subject_id=table_name4.subject_id
演出会有结束 2024-12-11 10:59:43

尝试:

   SELECT t1.given_name, t4.subject_name, t2.class, t2.main_id
     FROM t1
LEFT JOIN t2
       ON t1.student_id = t2.student_id
LEFT JOIN t3
       ON t2.main_id = t3.main_id
LEFT JOIN t4
       ON t3.subject_id = t4.subject_id
    WHERE t2.school_year = 2011 
      AND t2.sem = 1

Try:

   SELECT t1.given_name, t4.subject_name, t2.class, t2.main_id
     FROM t1
LEFT JOIN t2
       ON t1.student_id = t2.student_id
LEFT JOIN t3
       ON t2.main_id = t3.main_id
LEFT JOIN t4
       ON t3.subject_id = t4.subject_id
    WHERE t2.school_year = 2011 
      AND t2.sem = 1
鸠魁 2024-12-11 10:59:43
select t1.given_name, t4.subject_name, t2.class, t2.main_id
  from t1 
  join t2 on t1.student_id = t2.student_id
  join t2.main_id = t3.main_id
  join t3.subject_id = t4.subject_id
select t1.given_name, t4.subject_name, t2.class, t2.main_id
  from t1 
  join t2 on t1.student_id = t2.student_id
  join t2.main_id = t3.main_id
  join t3.subject_id = t4.subject_id
尴尬癌患者 2024-12-11 10:59:42

我想你需要这样的查询:

SELECT t1.given_name, t4.subject_name, t2.main_id
FROM t1
LEFT JOIN t2 ON t1.student_id = t2.student_id
LEFT JOIN t3 ON t2.main_id = t3.main_id
LEFT JOIN t4 ON t3.subject_id = t4.subject_id

I guess you need a query like this:

SELECT t1.given_name, t4.subject_name, t2.main_id
FROM t1
LEFT JOIN t2 ON t1.student_id = t2.student_id
LEFT JOIN t3 ON t2.main_id = t3.main_id
LEFT JOIN t4 ON t3.subject_id = t4.subject_id
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文