简体   繁体   中英

Formation of a SQL query

I have 3 tables:

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

Shop      (shopID(PK), shopName, shopAddress) 

Comment   (EmployeeNo(FK), employeeComments) 

Is it possible to run a query showing the names of all the employees who have left a comment in the comments table even if the employee name is not in the table?

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

I think it is a simple join which you have to use.

It is actually a small query that you can easily learn.

Following query will return Employee Name who left the comment and the Comment left by him

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

Try this:

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

To display the employees who have commented we have following query:

SELECT DISTINCT e.employeeName
FROM Employee e
WHERE e.EmployeeNo IN (SELECT EmployeeNo
                              FROM Comment) 
SELECT E.employeeName
FROM Employee E
INNER JOIN Comment C ON E.EmployeeNo = C.EmployeeNo;
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]
                      ) 
    select e.employeeName,
           c.employeeComment 
    from Employee e
    right join Comment c on
    e.EmployeeNo=c.EmployeeNo

this will give you employee name & comment

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM