Table1:
id - name - address
-----------------------------
1 - Jim - Some Street
2 - Adam - Some Street
3 - ABC - Some Street
Table2:
id - job - finished_by
---------------------------
1 - 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