SQL 如何使用 LEFT JOIN 从表中选择单个值,在 Google Datastudio 中

Posted

技术标签:

【中文标题】SQL 如何使用 LEFT JOIN 从表中选择单个值,在 Google Datastudio 中【英文标题】:SQL how to select a single value from a table with LEFT JOIN, In Google Datastudio 【发布时间】:2021-03-30 16:17:58 【问题描述】:

我有两张表,一张名为 mailing_events,一张名为 mailing_outletcontact。我的表格示例如下。

邮寄表格非常简单,没有重复:

+-------+------------+--------------------------+
|  id   | mailing_id |          email           |
+-------+------------+--------------------------+
| name1 |         12 | name1.company@gmail.com  |
| name2 |         15 | name2@gmail.com          |
| name3 |         20 | name3@gmail.com          |
+-------+------------+--------------------------+

我的第二个表“mailing_outletcontact”在电子邮件字段中有重复项。

+----+-------------------------+------------------+--------------+
| 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 字段最高的那一行。

我的代码是:

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 中运行 SQL,所以错误消息不是很好。在线查看后,错误 ID 也没有提供任何值。错误信息是:

The query returned an error.

Error ID: 3ab6a2cd

第二次编辑: shawnt00 提供的答案在 DBeaver 等 SQL 客户端软件中确实有效。因此,如果您在阅读本文时遇到了类似的问题,应该会有所帮助。

它仍然无法在 Datastudio 中使用他们的 SQL 连接,所以他们可能使用不同的标准或其他东西?

【问题讨论】:

请解释您的问题。 “这不起作用”没有用。 我更新了描述来解决这个问题。 您使用的是哪个 DBMS? 仅供参考:Google Data Studio 可以连接到不同的数据引擎,在这种情况下,它显然是连接到 mysql,这是尝试答案中许多语法错误的根源(我现在添加了 MySql 标签)。我在这里注意到这一点是因为 1) 直到现在我还不知道这一点,以及 2) 这样在未来,其他 SQL 响应者将意识到在继续之前坚持要知道 DBMS 引擎是什么(也就是说,Google Data Studio 不是本身就是数据引擎)。 【参考方案1】:
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;

【讨论】:

我收到以下错误:查询返回错误。错误 ID:b9550714 @WasHere 尝试删除两个地方的as 我收到错误“查询返回错误。错误 ID:615975fb” 我用 DBeaver 对数据库进行了 SQL 测试,它工作正常。但是,它仍然无法在带有 SQL 连接器的 Google Datastudio 中使用。我不确定他们是否有不同类型的 SQL?我会更新我的问题来解决这个问题。【参考方案2】:

架构和输入语句:

 create table mailing_events  (id  varchar(50), mailing_id int ,email varchar(50))
 insert into mailing_events values('name1', 12, 'name1.company@gmail.com');
 insert into mailing_events values('name2', 15, 'name2@gmail.com');
 insert into mailing_events values('name3', 20, 'name3@gmail.com');
     
 create table mailing_outletcontact( id int,email varchar(50),outletcontact_id int, email_number int);
 
 insert into mailing_outletcontact values(1,  'name1.company@gmail.com'                 ,6,             5 );
 insert into mailing_outletcontact values(2,  'name1.company@gmail.com'                 ,6,             6 );
 insert into mailing_outletcontact values(3,  'name1.company@gmail.com'                 ,6,             7 );
 insert into mailing_outletcontact values(4,  'name2@gmail.com'                         ,8,             8 );
 insert into mailing_outletcontact values(5,  'name3@gmail.com'                         ,4,             9 );
 insert into mailing_outletcontact values(6,  'name2@gmail.com'                         ,8,            10 );

Query#0 带有 DBeaver 和 Oracle 的子查询

 SELECT
         me.mailing_id,
         me.email,
         (select outletcontact_id    from mailing_outletcontact mo where mo.email=me.email fetch first 1 rows only) outletcontact_id
     FROM
         mailing_events me

Query#1 带有 Mysql 的子查询(限制 1)(这将返回第一个 outletcontact_id )

 SELECT
     me.mailing_id,
     me.email,
     (select outletcontact_id    from mailing_outletcontact mo where mo.email=me.email limit 1) outletcontact_id
 FROM
     mailing_events me
     

Query#2 带有 SQL Server 的子查询(前 1 个)(这将返回第一个 outletcontact_id)

  SELECT
     me.mailing_id,
     me.email,
     (select top 1 outletcontact_id    from mailing_outletcontact mo where mo.email=me.email) outletcontact_id
 FROM
     mailing_events me
 


Output:
mailing_id email outletcontact_id
12 name1.company@gmail.com 6
15 name2@gmail.com 8
20 name3@gmail.com 4

Query#3 聚合(你可以使用 min() 得到最小的 outletcontact_id 或 max() 得到最大的 outletcontact_id )

 SELECT
         me.mailing_id,
         me.email,
         (select min(outletcontact_id )   from mailing_outletcontact mo where mo.email=me.email) outletcontact_id
   FROM
         mailing_events me
 GO

输出:

mailing_id email outletcontact_id
12 name1.company@gmail.com 6
15 name2@gmail.com 8
20 name3@gmail.com 4

带有左连接的查询#4

  SELECT
             me.mailing_id,
             me.email,
             t.outletcontact_id
       FROM
             mailing_events me
       Left Join 
       (select email, min(outletcontact_id )outletcontact_id    from mailing_outletcontact group by email
       )t
       on me.email = t.email
 GO

输出:

mailing_id email outletcontact_id
12 name1.company@gmail.com 6
15 name2@gmail.com 8
20 name3@gmail.com 4

dbhere

【讨论】:

我收到错误“查询返回错误。SQL 语句出错:'字段列表'中的未知列 'mailing_events.mailing_id' 错误 ID:d182c69d”。我对SQL不太了解,但是该字段肯定在数据库中。我能够使用 mailing_events.mailing_id 运行一个简单的选择查询。 抱歉,存在别名问题。我已经修复了这些。请立即检查。 我已经对您的示例输入实施了所有查询。请立即尝试。【参考方案3】:

尝试以下方法之一:

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)

SELECT DISTINCT
  mailing_events.mailing_id,
  mailing_events.email,
  mailing_outletcontact.outletcontact_id
FROM mailing_events
LEFT JOIN mailing_outletcontact
USING (email)

【讨论】:

我都试过了,但 Datastudio 吐出了两个“查询返回错误”的沼泽标准错误。 您能尝试在 Google BigQuery 本身中运行查询吗?也许这样我们会得到更多有用的错误信息。【参考方案4】:

如果我理解正确,您正在寻找这个:

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 

【讨论】:

尝试此操作后,我收到以下错误:查询返回错误。 SQL 语句错误:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 9 行的“from mailing_outletcontact where mailing_events.email = new_mailing_outletco”附近使用正确的语法错误 ID:7b878355 有什么想法吗? 这个错误是因为top 1 语法不是有效的MySQL。 @shawnt00 对,wasHere,查看更新后的答案

以上是关于SQL 如何使用 LEFT JOIN 从表中选择单个值,在 Google Datastudio 中的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 join 和 where 子句从表中删除? [复制]

sql中left join、right join、inner join有啥区别

从 LEFT JOIN 中选择最大的行

使用 LEFT JOIN 删除

Sql查询left join

SQL Multiple COUNT() 来自两个表,在一个 LEFT JOIN 中