mysql - 在 phpMyAdmin 中的数据库表上触发查询时出现问题。获取 0 行作为结果

标签 mysql sql

我正在使用 phpMyAdmin 来提供数据,但遇到了问题。我对数据标准化没有太多经验。但我正在尝试一些东西。

我的数据库中有一些表,如下所示:

-- Table name `Universities`
SrNo  ||  University
-----     ----------
 1         Harvard
 2         Boston
 3         Oxford
 4         Stanford 
 5         Dublin

我将上表创建为:

CREATE TABLE Universities (

    SrNo INT PRIMARY KEY AUTO_INCREMENT,
    University LONGTEXT NOT NULL
);  

现在我有另一个表:

--Table name `Courses`
SrNo  ||  Course
----      ------
  1       Maths
  2       Physics
  3       Computer Science
  4       Electronics
  5       Chemistry

我将上表创建为:

CREATE TABLE Courses (

    SrNo INT PRIMARY KEY AUTO_INCREMENT,
    Course LONGTEXT
);  

现在的问题是,一所大学提供 n 门类(class),其中每所大学的 n 可能相等,也可能不相等。每门类(class)均由 m 所大学提供,其中每个科目的 m 可能相同,也可能不同。

例如:

Harvard --> Maths, Electronics, Physics                 (3 subjects)
Boston  --> Maths, Computer Science                     (2 subjects)
Stanford -> Chemistry, Electronics, Computer Science    (3 subjects)
Oxford  --> Physics, Chemistry, Maths, Computer Science (4 subjects)
Dublin  -->                                             (0 subjects)

由于无法触发 JOIN 查询(​​根据我的知识限制)来提取大学名称及其提供的类(class),因此根据我老师的建议(或者可能是提示),我所做的是我创建了另一个表,用于存储 Universities.SrNoCourses.SrNo 的引用。

SrNo  ||  UnivID  ||  CourseID
----      ------      --------
  1          1            1    -- This means Harvard(SrNo=1 in table `Universities`) offers Maths(SrNo=1 in table `Courses`)
  1          1            4
  1          1            2
  1          2            1
  1          2            3
  1          4            5
  1          4            4
  1          4            3
  1          3            2
  1          3            4
  1          3            1
  1          3            3
  1          5          null

我将上表创建为:

CREATE TABLE Reference (

    SrNo INT PRIMARY KEY AUTO_INCREMENT,
    UnivID INT NOT NULL,
    CourseID INT,      -- m not giving this a not null constraint coz a university may offer none of the course mentioned in the table `Courses`
);

在此帮助下,我尝试提取大学名称和他们提供的类(class)名称。

就像如果我想列出哈佛大学提供的所有类(class),这就是我所做的:

SELECT Universities.University, Courses.Course FROM Universities, Courses, Reference
WHERE Universities.University="Harvard" AND Reference.UnivID = Universities.SrNo

但结果我得到 0 行。

我该怎么办?
1> 有没有办法在没有名为 Reference 的表的情况下获得此结果:

Harvard    Maths
Harvard    Electronics
Harvard    Physics  

2> 如果是,那么如何?如果不是,我应该如何构建 SQL 查询才能在 1 中获得上述结果?

最佳答案

您可以通过嵌套查询来解决这个问题:

Select U.University,C.Course from Universities U, Course C where C.SrNo in (
Select R.CourseID from Reference R where UnivId in (select U1.SrNo from Universities U1 where U1.University ="Stanford" ))

关于mysql - 在 phpMyAdmin 中的数据库表上触发查询时出现问题。获取 0 行作为结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27154015/

相关文章:

mysql - 如何在存储过程中调用多个列

php - 对行进行排序,然后从该集合中获取特定的 10 行

sql - 如何查找和更新包含上标字符的列

mysql - 错误1064#使用mysql Like关键字时

php - 在 php foreach 循环中从 mysql 获取数据的有效方法

php - 如何以自动递增且不重复的方式发布ID

android - 使用括号和 ? Android中的SQLite数据库查询()

sql - 为一对多表实现查找或插入

SQL门访问

mysql - 如何连接同一张表但条件不同的另一张表