形成SQL查询

时间:2013-12-31 04:47:07

标签: mysql sql select join inner-join

我有3张桌子:

Employee  (EmployeeNo(PK),shopID(FK1) employeeName, employeeAddress) 

Shop      (shopID(PK), shopName, shopAddress) 

Comment   (EmployeeNo(FK), employeeComments) 

是否可以运行查询,显示在评论表中留下评论的所有员工的姓名,即使员工姓名不在表中?

7 个答案:

答案 0 :(得分:1)

Select employeeName from employee,comment
where employee.employeeNo = comment.employeeNo

我认为这是一个你必须使用的简单连接。

答案 1 :(得分:1)

这实际上是一个很容易学习的小查询。

以下查询将返回留下评论的员工姓名和他留下的评论

SELECT employeeName, employeeComments
FROM Employee INNER JOIN Comment ON (Employee.EmployeeNo = Comment.EmployeeNo) 

答案 2 :(得分:0)

试试这个:

SELECT DISTINCT E.employeeName
FROM Employee E INNER JOIN Comment C ON E.EmployeeNo = C.EmployeeNo;

答案 3 :(得分:0)

要显示已评论的员工,我们有以下查询:

SELECT DISTINCT e.employeeName
FROM Employee e
WHERE e.EmployeeNo IN (SELECT EmployeeNo
                              FROM Comment) 

答案 4 :(得分:0)

SELECT E.employeeName
FROM Employee E
INNER JOIN Comment C ON E.EmployeeNo = C.EmployeeNo;

答案 5 :(得分:0)

SELECT DISTINCT e.EmployeeName
FROM [Employee] AS e 
RIGHT JOIN [Comment] AS c 
ON e.EmployeeNo = c.EmployeeNo

OR

SELECT e.employeeName
FROM [Employee] AS e 
WHERE e.EmployeeNo IN (
                        SELECT EmployeeNo
                        FROM [Comment]
                      ) 

答案 6 :(得分:0)

    select e.employeeName,
           c.employeeComment 
    from Employee e
    right join Comment c on
    e.EmployeeNo=c.EmployeeNo

这将为您提供员工姓名&评论