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 7812261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:26:55+00:00 2026-06-02T04:26:55+00:00

I am trying to run a query on table ‘apst_mailings’ storing the content of

  • 0

I am trying to run a query on table ‘apst_mailings’ storing the content of each newsletter we send to our subscribers. Each time we attempt to send the email to an individual we insert a row in apst_mailings_accuses reporting the time and state of the sending, and the ID of the newletter. I wanna list the newsletters and count the total sent and successfully sent for each.

    SELECT m.id AS code_mailing, 
    COUNT(a.adh_code) AS num, COUNT(b.adh_code) AS succes
    FROM apst_mailings AS m
        LEFT OUTER JOIN apst_mailings_accuses AS a
            ON a.id_mailing = m.id
        LEFT OUTER JOIN apst_mailings_accuses AS b
            ON b.id_mailing = m.id
            AND b.etat = 'succes'
    GROUP BY m.id

And it simply hangs the server forever. I have tried each join in separated queries and it worked without problem:

// Counts the email sent per mailing
    SELECT m.id AS code_mailing, 
    COUNT(a.adh_code) AS num
    FROM apst_mailings AS m
        LEFT OUTER JOIN apst_mailings_accuses AS a
            ON a.id_mailing = m.id
    GROUP BY m.id

    SELECT m.id AS code_mailing, 
    COUNT(b.adh_code) AS succes
    FROM apst_mailings AS m
        LEFT OUTER JOIN apst_mailings_accuses AS b
            ON b.id_mailing = m.id
            AND b.etat = 'succes'
    GROUP BY m.id

I could split my query but the reason why it doesn’t work really bothers me. Can anyone explain please?

Thanks!

  • 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-06-02T04:26:56+00:00Added an answer on June 2, 2026 at 4:26 am

    You can what you want in a much simpler way by using a single join and using SUM to perform a conditional count.

    SELECT
       m.id AS code_mailing, 
       COUNT(a.adh_code) AS num,
       SUM(a.etat = 'succes') AS succes
    FROM apst_mailings AS m
    LEFT OUTER JOIN apst_mailings_accuses AS a
    ON a.id_mailing = m.id
    GROUP BY m.id
    

    But the reason why your query doesn’t work is because you’re joining ALL the rows in subquery a with ALL the matching rows in subquery b in a huge cross join. This could generate a huge temporary result set, which is presumably why the query takes forever to terminate. And even if it did terminate, your counts would be completely off – they would be the product of the two counts.

    To solve it do the GROUP BY first. Then JOIN the result of that to your main table.

    SELECT
       m.id AS code_mailing, 
       IFNULL(a.num, 0) AS num,
       IFNULL(b.succes, 0) AS succes
    FROM apst_mailings AS m
    LEFT OUTER JOIN (
       SELECT id_mailing, COUNT(adh_code) AS num
       FROM apst_mailings_accuses 
       GROUP BY id_mailing
    ) a
    ON a.id_mailing = m.id
    LEFT OUTER JOIN (
        SELECT id_mailing, COUNT(adh_code) AS succes
        FROM apst_mailings_accuses
        WHERE etat = 'succes'
        GROUP BY id_mailing
    ) b
    ON b.id_mailing = m.id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to run a query that selects values from a table using
I'm trying to run a query on a table that we keep for transactions
I'm trying to run a query in SQL Server 2008 against a table where
I am trying to run a query on a dbase IV table from C#.
I'm trying to run a query of the type SELECT * FROM table WHERE
I'm trying to run an update query that updates one table based on rows
I'm trying to run the following query using phpMyAdmin: UPDATE TABLE x SET `number`
I'm trying to run a query over a table with around a 1 million
I'm having an issue when trying to run my project each time it builds.
I am trying to run this query: ALTER TABLE table DROP PRIMARY KEY, ADD

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.