Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4116948
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:46:26+00:00 2026-05-20T22:46:26+00:00

Here is my problem : I have 3 tables : account, account_event and account_subscription

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T22:46:26+00:00Added an answer on May 20, 2026 at 10:46 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my problem - I have 2 tables: WORKER, with columns |ID|OTHER_STAF| ,
I have a bit of an architecture problem here. Say I have two tables,
I have following tables : users accounts ( user has_one account ) pictures (
Here is the problem: I have two columns in a table that, for each
here is a simple problem. I have a table of 500 rows and what
Here's an example of my problem on jsFiddle. I have a table with striped
I have a problem with sorting items in table in PHP. Here is what
I have encountered a problem of placing data into the Table View. Here is
I have problem adding arraylist to list view, will explain about my problem here..
First, sorry for my bad english, I'm French. Here the problem : I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.