Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql SQL如何在Google Datastudio中从具有左联接的表中选择单个值_Mysql_Sql_Group By_Left Join_Google Data Studio - Fatal编程技术网

Mysql SQL如何在Google Datastudio中从具有左联接的表中选择单个值

Mysql SQL如何在Google Datastudio中从具有左联接的表中选择单个值,mysql,sql,group-by,left-join,google-data-studio,Mysql,Sql,Group By,Left Join,Google Data Studio,我有两个表一个表叫做mailing\u events,另一个表叫做mailing\u outletcontact。我的表格示例如下 邮件表非常简单,没有重复项: +-------+------------+--------------------------+ | id | mailing_id | email | +-------+------------+--------------------------+ | name1 | 1

我有两个表一个表叫做mailing\u events,另一个表叫做mailing\u 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提供的答案在SQL客户端软件(如DBeaver)中有效。因此,如果你阅读这篇文章时遇到了类似的问题,应该会有所帮助


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

如果我理解正确,您正在寻找以下内容:

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 

模式和输入语句:

 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 );
带有DBeaver和Oracle子查询的Query0

 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
查询1和Mysql限制1的子查询这将返回第一个outletcontact\u 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
     
  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:
查询2和SQL Server Top 1的子查询这将返回第一个outletcontact\u 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
     
  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:
邮寄地址 电子邮件 outletcontact\u id 12 名字1。company@gmail.com 6. 15 name2@gmail.com 8. 20 name3@gmail.com 4. 请尝试以下方法之一:

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)


请用你的问题解释这个问题。这不起作用是没有用的。我更新了描述来解决这个问题。您使用的是哪种DBMS?仅供参考:Google Data Studio可以连接到不同的数据引擎,在这种情况下,它显然是连接到MySql,这是我现在添加MySql标记的尝试答案中许多语法错误的来源。我在这里注意到这一点是因为1到现在我还不知道这一点,2所以在将来,其他SQL响应程序会意识到在继续之前坚持知道DBMS引擎是什么,也就是说,Google Data Studio本身不是数据引擎。SQL语句错误:您的SQL语法有错误;查看与您的MySQL服务器版本对应的手册,查看第9行的“from mailing_outletcontact where mailing_events.email=new_mailing_outletco”附近使用的正确语法错误ID:7b878355有什么错误吗?错误是因为前1个语法无效的MySQL。@shawnt00 right,wasHere,请参阅更新的应答。我收到以下错误:查询返回错误。错误ID:b9550714@WasHere尝试删除两个位置的单词。我收到错误,查询返回错误。错误ID:615975fbWorks for me:我用数据库的DBeaver测试了SQL,它成功了。然而,它仍然不能在Google Datastudio中使用SQL连接器。我不确定他们是否有不同类型的SQL?我将更新我的问题以解决这个问题。我尝试了这两种方法,但Datastudio给出了这两种方法的bog标准错误。查询返回了一个错误。您可以尝试在Google BigQuery本身中运行查询吗?也许那样我们会得到更多有用的错误信息。我得到了错误,查询返回了一个错误。SQL语句错误:“字段列表”中的未知列“邮寄”事件。邮寄id错误id:d182c69d。我对SQL不太了解,但这个字段肯定是在数据库中,就像那样。我可以使用mailing_events.mailing_id运行简单的select查询。很抱歉,出现了别名问题。我已经修好了。请现在检查。我已经对您的示例输入实现了所有查询。请现在试一试。