简体   繁体   English

MySQL LIMIT结果与INNER JOIN有超过2个表

[英]MySQL LIMIT results with INNER JOIN with more than 2 tables

I have a database where a user is allowed to have more than one email address. 我有一个数据库,允许用户拥有多个电子邮件地址。 I want to query the database to get all the instances of a job for that customer. 我想查询数据库以获取该客户的所有作业实例。 Each job is returning twice, one with each email. 每项工作都会返回两次,每封电子邮件一次。

Here is my query: 这是我的查询:

SELECT customers.first_name, customers.last_name, emails.email, services.title, services.description, jobs.status, jobs.job_id 
FROM customers 

INNER JOIN emails ON customers.user_id=emails.user_id 
INNER JOIN jobs ON customers.user_id=jobs.customer_id 
INNER JOIN services ON jobs.service_id=services.service_id;

It is returning something like this: 它返回这样的东西:

数据库

I want to return results with the first email only. 我想仅使用第一封电子邮件返回结果。 The row should not be duplicated for each email address the user has on file. 对于用户存档的每个电子邮件地址,不应重复该行。

How can this be done? 如何才能做到这一点?

You can use GROUP BY on SQL to get unique column value like follows. 您可以在SQL上使用GROUP BY来获取如下的唯一列值。

SELECT customers.first_name, customers.last_name, emails.email, services.title, services.description, jobs.status 
FROM customers 

INNER JOIN emails ON customers.user_id=emails.user_id 
INNER JOIN jobs ON customers.user_id=jobs.customer_id 
INNER JOIN services ON jobs.service_id=services.service_id;

GROUP BY jobs.job_id;

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

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