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

  • Home
  • SEARCH
  • 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 584539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:54:30+00:00 2026-05-13T14:54:30+00:00

I have 2 tables, campaigns and campaign_codes: campaigns: id, partner_id, status campaign_codes: id, code,

  • 0

I have 2 tables, campaigns and campaign_codes:

campaigns: id, partner_id, status

campaign_codes: id, code, status

I want to get a count of all campaign_codes for all campaigns WHERE campaign_codes.status equals 0 OR where there are no campaign_codes records for a campaign.

I have the following SQL, but of course the WHERE statement eliminates those campaigns which have no corresponding records in campaign_codes ( i want those campaigns with zero campaign_codes as well)

SELECT 
    c.id AS campaign_id, 
    COUNT(cc.id) AS code_count
FROM 
    campaigns c
LEFT JOIN campaign_codes cc on cc.campaign_id = c.id
WHERE c.partner_id = 4
AND cc.status = 0
GROUP BY c.id
  • 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-13T14:54:30+00:00Added an answer on May 13, 2026 at 2:54 pm

    I’d opt for something like:

    SELECT 
        c.id AS campaign_id, 
        COUNT(cc.id) AS code_count
    FROM 
        campaigns c
    LEFT JOIN campaign_codes cc on cc.campaign_id = c.id
    AND cc.status = 0 -- Having this clause in the WHERE, effectively makes this an INNER JOIN
    WHERE c.partner_id = 4
    GROUP BY c.id
    

    Moving the AND to the join clause makes the join succeed or fail, crucially keeping resulting rows in where there is no matching row in the ‘right’ table.

    If it were in the WHERE, the comparisons to NULL (where there is no campaign_code) would fail, and be eliminated from the results.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code in my model for Application, I'm trying to get all
I have 3 tables called Companies Contacts Campaigns Contacts has foreign key of companies.
I have 3 tables. users, campaigns, links. They have one-to-many relations. User have many
Note: All sample code is greatly simplified. I have a DLL defined as: using
I have two tables, Campaign and Advert with a one to many relationship. During
I have two tables, 1 indexing the details of an email campaign. The second
I have two tables: Campaigns, Campaign_statistics. I need to output campaigns list with nested
Right now I have a table called Campaigns that has many Hits, if I
I have tables like this: tblUsers int UserID string UserName tblUsersInRoles int UserID int
In my database I have tables that define types for example Table: Publication Types

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.