我的SQL查询是否正确?

时间:2016-07-24 10:26:49

标签: mysql sql

使用下表 http://i.imgur.com/rIMgFZC.png

检索与Mat David具有相同E_level值且与Mat David在同一商店工作的员工姓名。

SELECT E_fname,E_lname
FROM Employee E,
(SELECT E_Level
FROM Employee E
WHERE UPPER(E.E_lname) = ‘DAVID’
AND UPPER(E.Efname) = ‘MAT’) elevel,
(SELECT E_Store_ID
FROM Employee E
WHERE UPPER(E.E_lname) = ‘DAVID’
AND UPPER(E.Efname) = ‘MAT’
) estoreid
WHERE E.E_level = elevel.E_level
AND E.Store_ID = estoreid.Store_ID
AND UPPER(E.E_lname) != ‘DAVID’
AND UPPER(E.Efname) != ‘MAT’;



SELECT E_fname,E_lname
FROM EMPLOYEE E
WHERE E.E_level = 
(SELECT E_level
FROM Employee E
WHERE UPPER(E.E_lname) = ‘DAVID’
AND UPPER(E.Efname) = ‘MAT’)
AND E.E_Store_ID = 
(SELECT E_Store_ID
FROM Employee E
WHERE UPPER(E.E_lname) = ‘DAVID’
AND UPPER(E.Efname) = ‘MAT’)
AND UPPER(E.E_lname) != ‘DAVID’
AND UPPER(E.Efname) != ‘MAT’;

这些是我的尝试。我是对的吗?

1 个答案:

答案 0 :(得分:0)

SELECT E_fname,E_lname
  FROM Employee E
    WHERE E_level = (SELECT E_Level
                      FROM Employee E
                        WHERE UPPER(E.E_lname) = 'DAVID'
                          AND UPPER(E.E_fname) = 'MAT')
      AND E_store_ID = (SELECT E_Store_ID
                        FROM Employee E
                          WHERE UPPER(E.E_lname) = 'DAVID'
                            AND UPPER(E.E_fname) = 'MAT'
                        )
      AND UPPER(E.E_lname) != 'DAVID'
      AND UPPER(E.E_fname) != 'MAT';