SQL 查詢沒有返回任何值 (SQL query did not return any values)


問題描述

SQL 查詢沒有返回任何值 (SQL query did not return any values)

Hi i have the following table structure.

Professor (EMP ID,Name,Status,Salary,Age)
Course(Course ID,Course Name,Points)
Works(Course ID,EMP ID,Class ID)

I need to do the following.

Return List of Employees who have taken 2 different course M1 and M2  for the same class ‘class 10’

This is the query that i have written.

SELECT p.EmpID, p.Name, p.Status, p.Salary 
FROM professor p, course c, works w 
WHERE p.EmpID = w.EmpID
AND
w.CourseID = c.CourseID
AND
w.ClassID = 10
AND
c.CourseName IN ( SELECT CourseName FROM course WHERE CourseName = 'm1'
AND CourseName = 'm2')

But the query doesnot return any values even though there are data in the db.


參考解法

方法 1:

This problem is commonly called Relational Division

SELECT  a.EmpID, a.name
FROM    Professor a
        INNER JOIN Works b
            ON a.EmpID = b.EmpID AND b.ClassID = 10 
        INNER JOIN  Course c
            ON b.CourseID = c.CourseID
WHERE   c.CourseNAME IN ('M1', 'M2')
GROUP   BY a.EmpID, a.name
HAVING  COUNT(DISTINCT c.CourseNAME) = 2
  • SQL of Relational Division

方法 2:

The subquery

( SELECT CourseName FROM course WHERE CourseName = 'm1' AND CourseName = 'm2')

will return nothing. Look at the "AND"

(by user1844638John WooUdo Klein)

參考文件

  1. SQL query did not return any values (CC BY‑SA 3.0/4.0)

#SQL #MySQL






相關問題

如何組合表和視圖? (How do combine tables and views?)

Sql中的WHERE,結合兩個快速條件會成倍增加成本 (WHERE in Sql, combining two fast conditions multiplies costs many times)

Oracle : Выкарыстанне ўкладзенага запыту супраць выкарыстання адлюстравання (Oracle : Using nested query vs using mapping)

SQL在友誼表中插入值基於 (SQL insert value in friendship table based on)

SQL 查詢沒有返回任何值 (SQL query did not return any values)

PL/SQL 塊和循環練習 (PL/SQL block and LOOP exercise)

查找與日曆相比缺失的日期 (Find missing date as compare to calendar)

在 C# 中使用數據庫需要一些幫助 (Need some help working with databases in C#)

如何設計n多對多關係以使sql查詢更容易 (How to design n many to many relationship in order make sql query easily)

在 SQL 中從 3 個視圖創建一個視圖 (Creating a view from 3 views in SQL)

java while (resultset.next()) 不返回同一列中的所有數據 (java while (resultset.next()) does not return all data in the same column)

從訪問表單的文本字段傳遞開始和結束日期參數 (Pass start and end date parameter from text field of access form)







留言討論