简体   繁体   English

两个表的SQL区别查询

[英]SQL Distinct Query for Two Tables

Table1: 表格1:

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

Table2: 表2:

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. 在上面的两个表中,Table1.id和Table2.finished_by应该被链接。

For, eg in table 2, job ABC was finished by Adam. 例如,在表2中,作业ABC由亚当完成。

My objective is to select DISTINCT records from Table 2. 我的目标是从表2中选择DISTINCT记录。

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 SELECT DISTINCT finish_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. 但是,似乎只执行查询的第一行,因为我没有看到LEFT JOIN表。

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. 因此,从StacKOverflow的一些非常杰出的成员那里,我注意到我的逻辑是完全错误的。.因此,我将尝试用简单的单词而不是程序/代码来解释我要实现的目标。 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等这样的CompanyA人都在CompanyA工作。但是,CompanyA派Jim,Adam等来另一公司工作。

Jim, Adam etc can be sent to work for multiple such companies. 吉姆(Jim),亚当(Adam)等可以被派到多家此类公司工作。 Say Jim is sent to work for Company1 twice and Adam was sent to work for Company1 thrice. 假设Jim两次被派往Company1工作,而Adam被三次派往Company1工作。

Table 2 maintains records of how many time a person went to work for Company1 in the following format: 表2以以下格式维护一个人为Company1工作多少次的记录:

Table2: (Ref: Company1) 表2 :(参考: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: 现在,我的目标很简单,需要为Company1生成报告如下:

  • List the persons we sent to Company1 (in Alphabetic Order) 列出我们发送给Company1的人员(按字母顺序)
  • 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 还应该包括他在Company1工作期间在那所做的工作

For, eg an Ideal Output/Report would be: 例如,理想的输出/报告将是:

Name of Employee - Job Description - Dates 员工姓名-职位描述-日期

ABC - EFD - 11 Oct ABC-EFD-10月11日

Adam - ABC, XYZ - 10 Oct, 12 Oct 亚当-ABC,XYZ-10月10日,10月12日

Jim - BVC - 13 Oct 吉姆-BVC-10月13日

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 我可以完成所有基本报告,但是我不知道如何将column中位于表2中的数字转换为表1中的相应名称

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. MySQL的GROUP_CONCAT函数完成了这一任务。 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. 请记住,每个GROUP_CONCAT列表中的值没有顺序。 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. 听起来好像您想要所有Table1详细信息,以及它们在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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM