如何从 SELECT 子查询中获取多个列?

发布于 2024-10-28 13:00:42 字数 1529 浏览 3 评论 0原文

这是我的问题:

我有 3 个表:account、account_event 和 account_subscription

account 包含详细信息,例如:company_name、电子邮件、电话、...

account_event 包含以下事件:来电、拨出电话、访问、邮件

我在此查询中使用 account_subscription 来检索“潜在客户”帐户。如果该帐户没有订阅,则它是潜在客户。

我现在使用的是以下查询,该查询运行良好:

SELECT `account`.*,
    (SELECT event_date
     FROM clients.account_event cae
     WHERE cae.account_id = account.id
           AND cae.event_type = 'visit'
           AND cae.event_done = 'Y'
     ORDER BY event_date DESC
     LIMIT 1) last_visit_date
FROM (`clients`.`account`)
WHERE (SELECT count(*)
       FROM clients.account_subscription cas
       WHERE cas.account_id = account.id) = 0
ORDER BY `last_visit_date` DESC

您可以看到它返回last_visit_date

我想修改我的查询以返回上次事件详细信息(上次联系)。我需要 event_dateevent_type

因此,我尝试了以下查询,该查询不起作用,因为显然我无法从选择子查询中获取超过一列。

SELECT `account`.*,
        (SELECT event_date last_contact_date, event_type last_contact_type
         FROM clients.account_event cae
         WHERE cae.account_id = account.id
               AND cae.event_done = 'Y'
         ORDER BY event_date DESC
         LIMIT 1)
FROM (`clients`.`account`)
WHERE (SELECT count(*)
       FROM clients.account_subscription cas
       WHERE cas.account_id = account.id) = 0
ORDER BY `last_visit_date` DESC

我尝试了很多有关连接的解决方案,但我的问题是我需要获取每个帐户的最后一个事件。

有什么想法吗?

先感谢您。

杰罗姆

Here is my problem :

I have 3 tables : account, account_event and account_subscription

account contains details like : company_name, email, phone, ...

account_event contains following events : incoming calls, outgoing calls, visit, mail

I use account_subscription in this query to retrieve the "prospects" accounts. If the account does not have a subscription, it is a prospect.

What I am using right now is the following query, which is working fine :

SELECT `account`.*,
    (SELECT event_date
     FROM clients.account_event cae
     WHERE cae.account_id = account.id
           AND cae.event_type = 'visit'
           AND cae.event_done = 'Y'
     ORDER BY event_date DESC
     LIMIT 1) last_visit_date
FROM (`clients`.`account`)
WHERE (SELECT count(*)
       FROM clients.account_subscription cas
       WHERE cas.account_id = account.id) = 0
ORDER BY `last_visit_date` DESC

You can see that it returns the last_visit_date.

I would like to modify my query to return the last event details (last contact). I need the event_date AND the event_type.

So I tried the following query which is NOT working because apparently I can't get more than one column from my select subquery.

SELECT `account`.*,
        (SELECT event_date last_contact_date, event_type last_contact_type
         FROM clients.account_event cae
         WHERE cae.account_id = account.id
               AND cae.event_done = 'Y'
         ORDER BY event_date DESC
         LIMIT 1)
FROM (`clients`.`account`)
WHERE (SELECT count(*)
       FROM clients.account_subscription cas
       WHERE cas.account_id = account.id) = 0
ORDER BY `last_visit_date` DESC

I tried a lot of solutions around joins but my problem is that I need to get the last event for each account.

Any ideas?

Thank you in advance.

Jerome

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

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

发布评论

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

评论(2

千鲤 2024-11-04 13:00:42

在子查询中获取一个 PRIMARY KEY 并在其上连接实际表:

SELECT  a.*, ae.*
FROM   account a
JOIN   account_event ae
ON     ae.id =
       (
       SELECT  id
       FROM    account_event aei
       WHERE   aei.account_id = a.id
               AND aei.event_done = 'Y'
       ORDER BY
               event_date DESC
       LIMIT 1
       )
WHERE  a.id NOT IN
       (
       SELECT  account_id
       FROM    account_subscription
       )
ORDER BY
       last_visit_date DESC

Get a PRIMARY KEY in a subquery and join the actual table on it:

SELECT  a.*, ae.*
FROM   account a
JOIN   account_event ae
ON     ae.id =
       (
       SELECT  id
       FROM    account_event aei
       WHERE   aei.account_id = a.id
               AND aei.event_done = 'Y'
       ORDER BY
               event_date DESC
       LIMIT 1
       )
WHERE  a.id NOT IN
       (
       SELECT  account_id
       FROM    account_subscription
       )
ORDER BY
       last_visit_date DESC
牵你手 2024-11-04 13:00:42

尝试将子查询移至 from 部分并为其指定别名;它看起来就像是另一个表,您将能够从中提取多个列。

Try moving the subquery to from part and alias it; it will look as just another table and you'll be able to extract more than one column from it.

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