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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:36:56+00:00 2026-05-26T11:36:56+00:00

I have some tables that have ‘has and belongs to many’ type relationships using

  • 0

I have some tables that have ‘has and belongs to many’ type relationships using join tables. The main tables are countries, programs and listings. The basic structure is:

countries
  -id
  -name

programs
  -id
  -name

listings
  -id
  -title

countries_programs
  -country_id
  -program_id

listings_programs
  -listing_id
  -program_id

listings_countries
  -listing_id
  -country_id

I have been doing the following to list the programs for a country when given the country id value (in this case 5):

SELECT programs.* 
FROM programs 
LEFT JOIN countries_programs ON programs_id = countries_programs.program_id 
LEFT JOIN countries ON countries.id = countries_programs.country_id 
WHERE countries_id = '5'

What I need to do is only return programs for the country only if the programs for the specific country actually have any listings that are also in that country. So it should return only the case where the programs are in the country specified and have listings that are in that program and also in that country.

If a program for the specified country has no listings, then I don’t want it returned. I’ve been trying various combinations, but can’t seem to get this to work. Does anyone see how this could be done?

I think I need to join the listings table, but nothing I’ve tried has come close.

  • 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-26T11:36:57+00:00Added an answer on May 26, 2026 at 11:36 am

    To avoid returning duplicate data, I’d use an EXISTS clause. Also, switch to INNER joins to satisfy the country requirement.

    SELECT p.* 
    FROM programs p
    INNER JOIN countries_programs cp
        ON p.id = cp.program_id 
    WHERE cp.country_id = 5
    AND EXISTS (
        SELECT 1 FROM listings_countries lc
        INNER JOIN listings_programs lp
            ON lc.listing_id = lp.listing_id
        WHERE lc.country_id = c.id
        AND lp.program_id = p.id
    )
    

    I’ve omitted the join to countries as you’re only dealing with the country ID which is available in countries_programs.

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

Sidebar

Related Questions

I have some tables that benefit from many-to-many tables. For example the team table.
Greetings, I have some mysql tables that are currently using an md5 hash as
I'm using MySQL 4.1. Some tables have duplicates entries that go against the constraints.
I am using MembershipServices. I have some other tables that I need to associate
I have some tables that has the following structure: cat( cat_id(pk), cat_name) ven( ven_id(pk),
We're currently using MySQL with innodb and we have some large tables that are
I have just started using jqGrid , and I have some tables that I
I have some tables in my database that have primary keys of type int
I'm using SQL Server (2005). Today I have some tables that everytime the data
I'm using playframework 1.2.4, and mysql database. I have some tables that are in

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.