首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SQL如何在中从带左联接的表中选择单个值

SQL如何在中从带左联接的表中选择单个值
EN

Stack Overflow用户
提问于 2021-03-30 16:17:58
回答 3查看 493关注 0票数 1

我有两个表,一个是mailing_events表,一个是mailing_outletcontact表。下面是我的表格的例子。

邮寄表非常简单,没有副本:

代码语言:javascript
复制
+-------+------------+--------------------------+
|  id   | mailing_id |          email           |
+-------+------------+--------------------------+
| name1 |         12 | name1.company@gmail.com  |
| name2 |         15 | name2@gmail.com          |
| name3 |         20 | name3@gmail.com          |
+-------+------------+--------------------------+

我的第二个表"mailing_outletcontact“在电子邮件字段中有副本。

代码语言:javascript
复制
+----+-------------------------+------------------+--------------+
| id |          email          | outletcontact_id | email_number |
+----+-------------------------+------------------+--------------+
|  1 | name1.company@gmail.com |                6 |            5 |
|  2 | name1.company@gmail.com |                6 |            6 |
|  3 | name1.company@gmail.com |                6 |            7 |
|  4 | name2@gmail.com         |                8 |            8 |
|  5 | name3@gmail.com         |                4 |            9 |
|  6 | name2@gmail.com         |                8 |           10 |
+----+-------------------------+------------------+--------------+

我试图查询Datastudio中的数据库,我的目标是用我的第一个表数据获取"outletcontact_id“字段。

但是,我尝试做一个左联接,因为第二个表中有多个值,所以我必须选择一个行来匹配。对我来说,它匹配的行并不重要,我决定选择一个具有最高id字段的行。

我的代码是:

代码语言:javascript
复制
SELECT
    mailing_events.mailing_id,
    mailing_events.email,
    new_mailing_outletcontact.outletcontact_id
FROM
    mailing_events
LEFT JOIN(
    select  *
    from  mailing_outletcontact
    where id in(select max(id) from mailing_outletcontact group by email)   
) as new_mailing_outletcontact 
    on mailing_events.email = new_mailing_outletcontact.email;
    
SELECT
    mailing_events.mailing_id,
    mailing_events.email,
    new_mailing_outletcontact.outletcontact_id
FROM
    mailing_events
LEFT JOIN(
    select *
    from mailing_outletcontact
    where id in(select max(id) from mailing_outletcontact group by email)   
) as new_mailing_outletcontact 
    on mailing_events.email = new_mailing_outletcontact.email;

这不管用,有人知道我哪里出了问题吗。或者如何彻底解决我的问题。它使用我的第一个表数据获取"outletcontact_id“字段。

编辑:,我在Datastudio中运行,所以错误消息不是很好。在联机查看后,错误ID也不会提供任何值。错误信息是:

代码语言:javascript
复制
The query returned an error.

Error ID: 3ab6a2cd

第二次编辑: shawnt00提供的答案在SQL软件(如DBeaver )中工作。所以,如果你是用类似的问题来阅读这篇文章的话,那应该是有帮助的。

在使用SQL连接的Datastudio中,它仍然不能工作,所以他们可能会使用不同的标准或其他什么?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2021-03-30 16:42:13

代码语言:javascript
复制
SELECT
    me.mailing_id, me.email,
    (
        select max(moc.outletcontact_id)
        from mailing_outletcontact moc
        where moc.email = me.email
    ) as outletcontact_id
FROM
    mailing_events me;
票数 1
EN

Stack Overflow用户

发布于 2021-03-30 16:25:51

如果我没听错的话,你要找的是:

代码语言:javascript
复制
SELECT
    mailing_events.mailing_id,
    mailing_events.email,
    new_mailing_outletcontact.outletcontact_id
FROM
    mailing_events
CROSS JOIN (
    select *
    from  mailing_outletcontact
    where mailing_events.email = new_mailing_outletcontact.email
    order by mailing_outletcontact.Id DESC
    LIMIT 1
) as new_mailing_outletcontact 
票数 0
EN

Stack Overflow用户

发布于 2021-03-30 16:44:35

试一试其中一种:

代码语言:javascript
复制
SELECT
  mailing_events.mailing_id,
  mailing_events.email,
  new_mailing_outletcontact.outletcontact_id
FROM
  mailing_events
LEFT JOIN (
  select distinct email, outletcontact_id
  from  mailing_outletcontact
) as new_mailing_outletcontact 
USING (email)

代码语言:javascript
复制
SELECT DISTINCT
  mailing_events.mailing_id,
  mailing_events.email,
  mailing_outletcontact.outletcontact_id
FROM mailing_events
LEFT JOIN mailing_outletcontact
USING (email)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66874699

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档