mysql - How to use aggregation functions with multiple joins? -


I need to find the maximum work experience of the employee meet . This is my table:

  Make Tables Company (id INT not original primary key AUTOCCREMENT, name VARCHAR (250)) Faucet; Create table worker (id INT not original primary key AUTO_INCREMENT, name VARCHAR tap (250)); Make Table Company (Employee) (Company_ID not INT, Employee_ID not INT, Rent_Data Date, Error Zero, Regina_Data DATE DEFUILT Faucet, Foreign Key (company_id) Reference Company (ID), Foreign Key (employee_id) Reference Person (ID);   

and my query that meets all the companies of Meet

  select Employee. Name, company Join the company (company.id = company_employee.company_id) employee at JOIN company_employee (company_employee.employee_id = employee.id) where the employee can join the company, company_employee.hire_date, company_employee.resign_date, (company_employee.resign_date - company_employee.hire_date). Name = 'Meet'   

This Return

  + ------ + -------------- + ----------- + - ----------- + ----------------- + | Name | company_name | Rent_date | resign_date | Experience_de + ------ + -------------- + ----------- + ------------- + - ---------------- + | Meet | IBM | 1997-04-17 | 1998-03-08 | 325 | | Meet | IBM | 2012-04-03 | 2014-02-15 | 683 | | Meet | IBM | 2000-08-10 | 2003-01-01 | 874 | + ------ + -------------- + ----------- + ------------- + - ---------------- +   

But I want only one record with maximum experience

  + - ---- + ----- + ------------ + ------------ + ----- + | Meet | IBM | 2000-08-10 | 2003-01-01 | 874 | + ------ + ----- + ------------ + ------------ + ----- +   

UPD:

This query finds the number of employees of each company

  SELECT company.name, COUNT (company_employee.employee_id) from the company LEFT JOIN company_id = company_employee.company_id GROUP BY company.name at company_employee Hagueing COUNT (company_employee.employee_id) & gt; 0;   

Can I simplify it?

You do not need aggregation work ( MAX ) because you are here Also,

You can simply add ORDER BY and LIMIT sections like:

  ORDER BY experience_days DESC LIMIT 1   

The entire query might be:

  Select the employee. Name, company Name, company_employee.hire_date, company_employee.resign_date, (company_employee.resign_date - company_employee) company joint company joint company admin (company.id = company_moribi.com) or as employee of company (company_my employee employee / id = employee .ID). / Pre> 

@update

Yes you can make your query easier.

First of all, you can call JOIN You can use instead of LEFT

After this, because you already have only employees (The employee line should be present to join the company), you do not need to use the HAVING condition.

  SELECT company.name Company company.Momorporary.EMPAG_ID Company company company company company id.money company.morphy.company_id group company company name;    

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -