[英]SQL how to select a single value from a table with LEFT JOIN, In Google Datastudio
我有兩張表,一張叫 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”在 email 字段中有重復項。
+----+-------------------------+------------------+--------------+
| 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
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;
模式和輸入語句:
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
查詢#2 帶有子查詢 SQL 服務器(前 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 | 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
Output:
mailing_id | 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
Output:
mailing_id | outletcontact_id | |
---|---|---|
12 | name1.company@gmail.com | 6 |
15 | name2@gmail.com | 8 |
20 | name3@gmail.com | 4 |
db<小提琴在這里
如果我理解正確,您正在尋找這個:
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
嘗試其中之一:
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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.