简体   繁体   中英

Checking if mysql query provides the best solution to the question

List the Employee names and address of all the employees working in the EmployeeType whose code is 1 and the hourly rate of pay is €40.

I would like to know if this is the best way of retrieving information from the question above

SELECT e.EmployeeName , e.EmployeeAddress
FROM Employee e JOIN EmployeeType et ON e.EmployeeType = et.EmployeeType
HAVING e.EmployeeCode = (1) AND et.TotalPay = (40);

I wouldn't use 'HAVING' in this query, I would prefer WHERE condition or add more parameters on join. You can read about HAVING on this LINK Having is little bit different then WHERE condition, because its applied after querying results.

1st solution with simple WHERE condition

SELECT e.EmployeeName , e.EmployeeAddress
FROM Employee e 
INNER JOIN EmployeeType et ON e.EmployeeType = et.EmployeeType
WHERE e.EmployeeCode = '1' AND et.TotalPay = '40';

2nd solution add more parameters on join

SELECT e.EmployeeName , e.EmployeeAddress
FROM Employee e 
INNER JOIN EmployeeType et ON (e.EmployeeType = et.EmployeeType AND et.TotalPay = '40')
WHERE e.EmployeeCode = '1' ;

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