MySQL - Using JOIN and WHERE to determine average hours

前端 未结 4 1214
死守一世寂寞
死守一世寂寞 2021-01-22 13:15

I am learning MySQL using a next.tech course that relies on the following schema:

My current task is to find the average number of hours worked on one specific

相关标签:
4条回答
  • 2021-01-22 13:45

    In the above Query, you are trying to compare employee_id and project_id, i think that might be the issue. Make sure that you are using JOIN correctly with attributes.

    Thank you for reading!! Happy Learning!!

    0 讨论(0)
  • 2021-01-22 13:45

    The where clause must come after join, not before

    0 讨论(0)
  • 2021-01-22 13:57

    You need specific wich join you using, there's INNER,LEFT,RIGHT,FULL,OUTER So try these

    SELECT AVG(hours) FROM project_employees
    INNER JOIN  projects
    ON  project_employees.employee_id = projects.id
    WHERE name = 'Washington Avenue Barber';
    
    0 讨论(0)
  • 2021-01-22 14:01

    The syntax of your SQL query seems to be the main problem behind the first error.

    you are doing SELECT-FROM-WHERE-JOIN, but the actual sequence should be SELECT-FROM-JOIN-WHERE.

    Your second query (which is returning a wrong value) has the correct syntax, which is why it returns something.

    Secondly, you should be comparing projects.id with project_employees.project_id, not project_employees.employee_id. Therefore, your query should be something like -

    SELECT AVG(hours) FROM project_employees
    JOIN  projects
    ON  project_employees.project_id = projects.id
    WHERE name = 'Washington Avenue Barber';
    
    0 讨论(0)
提交回复
热议问题