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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:34:30+00:00 2026-06-09T15:34:30+00:00

I have been researching the answer to my question for two days now and

  • 0

I have been researching the answer to my question for two days now and I have not had any luck, probably because I don’t know exactly how to describe what I am looking for, as far as the methods I need to use.

Basically within my database, I have two tables, one named tblCounties (ID, County_vc), the other tblOrganizations (ID, Organization_vc). I have a third table which links the two together, called jnCountyOrg (Org_fk_id, County_fk_id). One organization can belong to a minimum of one county, up to a maximum of three counties. When I do a while loop through my select query using php, it echo’s the results properly like this:

CountyName1CountyName2CountyName3

I have tried using mysql_fetch_array and mysql_fetch_assoc.

I would like the results to be formatted like so.

IF only one county is associated with an organization, echo “xxx County”.

IF two counties associated with organization, then echo “xxx and xxx Counties”.

Lastly, if three counties (which is the max) are associated with the organization, then “xxx, xxx and xxx Counties”.

I am confused as to whether I am to do this in the SQL using CASE statements, or if I should use php to format my results in this fashion after they are queried from the database. Any help would be greatly appreciated.

  • 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-09T15:34:31+00:00Added an answer on June 9, 2026 at 3:34 pm

    Here is a SQLFiddle example. It works also for county count > 3:

    select id,CompanyNAme,countyCount,countyNAmes,
    if(countyCount=1,concat(countyNAmes,' County'),
       if(countyCount=2,concat(REPLACE(countyNAmes,', ',' and '),' Counties'),
    
             CONCAT(SUBSTRING_INDEX(countyNAmes,',',countyCount-1)
                ,' and '
                , SUBSTRING_INDEX(countyNAmes,',',-1)
                ,'  Counties'
                )
          )
    ) RESULT
    from
    (
    select o.id,max(o.Organization_vc) CompanyNAme,
    group_concat(c.County_vc separator ', ') CountyNames,count(c.id) CountyCount 
    
    from tblOrganizations o
    left join jnCountyOrg co on (o.id=co.Org_fk_id) 
    left join tblCounties c on (co.County_fk_id = c.id)
    group by o.id
    ) m
    order by m.id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been researching online, but could not find the answer. In Symfony2, I
I have been researching this for a while but got no convinced answer. From
I have been Google-ing and researching for an answer on how to do a
I am beyond frustrated. I have been researching an answer to this for hours
I've been researching this for a while now and cant get a straight answer.
This topic has been covered over and over, but after two days of researching
I've been researching this for quite long time now, can't find the answer. How
i have been researching on this topic and havent found any answers yet. Im
I have been researching for the last two hours, and have found nothing. Basically,
I have been researching for sometime now regarding the process to install android sdk

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.