一个查询连接一张表中的多行

发布于 2024-10-05 07:49:35 字数 492 浏览 4 评论 0原文

好的,我在 MySQL 中有两个表。一张表保存客户信息,另一张表保存电话号码。我需要在一个查询中加入这些表,以从电话表中选择最多两个电话号码以及客户信息。现在我的查询是:

SELECT customers.name, phones.phone, phones2.phone 
FROM customers 
LEFT JOIN phones ON phones.customerid=customers.id 
LEFT JOIN phones AS phones2 ON phones2.customerid=customers.id 
GROUP BY customers.id;

但是,这会为电话 1 和电话 2 返回相同的电话号码。本质上我需要做的是将 Phone2 偏移 1,但我不知道如何在语法上做到这一点。

电话位于单独的表中,因为它是一对多的关系。

我需要将其包含在一个查询中,因为我将其直接导出到 csv。

非常感谢您的帮助。提前致谢。

Ok, so I have two tables in MySQL. One table holds customer information, the other holds phone numbers. I need to join these tables in one query to select up to two phone numbers from the phones table as well as customer information. right now my query is:

SELECT customers.name, phones.phone, phones2.phone 
FROM customers 
LEFT JOIN phones ON phones.customerid=customers.id 
LEFT JOIN phones AS phones2 ON phones2.customerid=customers.id 
GROUP BY customers.id;

However, this returns the same phone number for phone 1 and phone 2. essentially what I need to do is offset phones2 by 1, but I don't know how to do that syntactically.

The phones are in a separate table because it's a one to many relationship.

I need this to be in one query because I'm exporting this directly to a csv.

Help is much appreciated. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暖伴 2024-10-12 07:49:36

您想要的是删除数字相同的情况。

SELECT customers.name, phones.phone, phones2.phone 
FROM customers 
LEFT JOIN phones ON phones.customerid=customers.id 
LEFT JOIN phones AS phones2 ON phones2.customerid=customers.id 
WHERE phones2.phone != phones.phone
GROUP BY customers.id

What you want is to do remove the cases where the numbers are the same.

SELECT customers.name, phones.phone, phones2.phone 
FROM customers 
LEFT JOIN phones ON phones.customerid=customers.id 
LEFT JOIN phones AS phones2 ON phones2.customerid=customers.id 
WHERE phones2.phone != phones.phone
GROUP BY customers.id
衣神在巴黎 2024-10-12 07:49:35

那么您只想为每个客户提供任意 0、1 或 2 个电话号码吗?如果是的话 MaxMin 可以吗?这将为您节省一个加入时间。

SELECT customers.name, 
       MAX(phones.phone) AS phone, 
       CASE WHEN MAX(phones.phone) = MIN(phones.phone) THEN NULL ELSE MIN(phones.phone) END AS phone2
FROM customers 
LEFT JOIN phones ON phones.customerid=customers.id 
GROUP BY customers.id;

You just want an arbitrary 0,1, or 2 phone numbers per customer then? If so Max and Min OK? This will save you a join.

SELECT customers.name, 
       MAX(phones.phone) AS phone, 
       CASE WHEN MAX(phones.phone) = MIN(phones.phone) THEN NULL ELSE MIN(phones.phone) END AS phone2
FROM customers 
LEFT JOIN phones ON phones.customerid=customers.id 
GROUP BY customers.id;
a√萤火虫的光℡ 2024-10-12 07:49:35

为了避免两次获得相同的电话号码,您可以更改以下内容:

LEFT JOIN phones AS phones2 ON phones2.customerid=customers.id 

为此:

LEFT JOIN phones AS phones2 ON phones2.customerid=customers.id
AND phones2.phone <> phones.phone

To avoid getting the same phone number twice you could change this:

LEFT JOIN phones AS phones2 ON phones2.customerid=customers.id 

To this:

LEFT JOIN phones AS phones2 ON phones2.customerid=customers.id
AND phones2.phone <> phones.phone
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文