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.
Here is a SQLFiddle example. It works also for county count > 3: