开发者

SQL Distinct Query for Two Tables

Table1:

id  -  name  -  address
-----------------------------
1   -  Jim   -  Some Street    
2   -  Adam  -  Some Street    
3   -  ABC   -  Some Street

Table2:

id  -  job  -  finished_by
---------------------------
1开发者_JAVA技巧   -  ABC  -  2    
2   -  EFD  -  3    
3   -  XYZ  -  2    
4   -  BVC  -  1

In the above two tables Table1.id and Table2.finished_by are supposed to be linked.

For, eg in table 2, job ABC was finished by Adam.

My objective is to select DISTINCT records from Table 2.

and the result should output all the job completed by each of the persons.

I have this query so far:

   SELECT *
     FROM table2
LEFT JOIN table1 ON table2.finished_by = table1.id
     LIMIT 0 , 30

This joins the Tables side by side, but how do i edit the query to make it display only distinct records, so that the output is:

id  -  job  -  id  -  name
----------------------------
1   -  ABC  -  2  -  Adam    
2   -  EFD  -  3  -  ABC    
4   -  BVC  -  1  -  Jim

Update:

So, i've did some googling and made some changes to my query:

SELECT DISTINCT finished_by FROM table2 LEFT JOIN table1 ON table2.finished_by = table1.id LIMIT 0 , 30

But, it seems that only first line of the query is executed since, i dont see the LEFT JOIN table.

May be this query needs a bit more finishing??

More Updates:

So, from some very distinguished members of StacKOverflow it has been brought to my notice that my logic is totally wrong.. So, i'll try to explain what i am trying to achieve in simple words and not program/code. May be that way i can be fetch a quick solution.

So, there's my Company: CompanyA people like Jim, Adam etc work for CompanyA.. But, CompanyA sends Jim, Adam etc.. to work for another Company.. Say Company1

Jim, Adam etc can be sent to work for multiple such companies. Say Jim is sent to work for Company1 twice and Adam was sent to work for Company1 thrice.

Table 2 maintains records of how many time a person went to work for Company1 in the following format:

Table2: (Ref: Company1)

id  -  job  -  finished_by  -  Date  
------------------------------------
1   -  ABC  -  2    -  10 Oct
2   -  EFD  -  3    -  11 Oct
3   -  XYZ  -  2    -  12 Oct 
4   -  BVC  -  1    -  13 Oct

Now, my objective is simple, The reports need to be generated as follows for Company1:

  • List the persons we sent to Company1 (in Alphabetic Order)
  • This list should include No. of times the person went (and Dates)
  • Should also Include the job he did there while he was working for Company1

For, eg an Ideal Output/Report would be:

Name of Employee - Job Description - Dates

ABC - EFD - 11 Oct

Adam - ABC, XYZ - 10 Oct, 12 Oct

Jim - BVC - 13 Oct

I can do all the basic reporting, But i judt dont know how to Convert the numbers that are sitting into Table2 in finished_by coloumn into their respective names from table1

I hope i'm clear with my question now.

Thanks, Everyone!!

I really appreciate your time and effort


Based on your latest update, it sounds like you want a comma-separated list of the "job" names and dates. MySQL's GROUP_CONCAT function accomplishes that. So perhaps something like this:

SELECT table1.*, GROUP_CONCAT(table2.job), GROUP_CONCAT(table2.date)
FROM table1
INNER JOIN table2 ON (t1.id = t2.finished_by)
GROUP BY t1.id

This will give you a list of all employees who did work, along with comma-separated lists of where they did work and when.

Keep in mind that there's no order to the values in each GROUP_CONCAT list. So you can't be sure, for example, that the first job listed corresponds to the first date listed. But if you wanted to keep that connection intact you'd want each job in a separate row anyway.


SELECT DISTINCT *
FROM table2
LEFT JOIN table1 ON table2.finished_by = table1.id
LIMIT 0 , 30

does this work?


It sounds as though you want all Table1 details, together with a count of all jobs they have finished from Table2. If so, try this:

select t1.id, 
       max(t1.name)    name, 
       max(t1.address) address, 
       count(t2.id)    finished_jobs
from Table1 t1 left outer join Table2 t2 on t1.id = t2.finished_by
group by t1.id;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜