Jump to content

Recommended Posts

SQL Server version: 5.0.85

 

Table Name: Instructors

---------------------------------------

Name  | City          |    City1

John    Liverpool      Manchester

Ian      Manchester 

Paul    London       

---------------------------------------

Hi,

 

As shown above I have a table where members are registered and they can choose up two towns to be assigned to. Paying members can be assigned to two towns whilst free members can only be assigned to one town.

 

What i want to do is count how many members are displayed in each town and then order this by the most popular towns. For example the result of the above should be:

 

City              | Count

Manchester    2

Liverpool        1

London          1

 

Currently i am using

 

SELECT City, COUNT(City) as count
FROM Instructors
GROUP BY City
ORDER by count DESC
LIMIT 5

 

 

Which works great if there are no paying members as it will count the City column. However now i have paying members I need the count to include the City1 column also.

 

Any thoughts?

Link to comment
https://forums.phpfreaks.com/topic/187499-counting-distinct-columns/
Share on other sites

Thanks for your suggestions.

 

So after reading the article am I on the right lines with taking the City field out of the table.

 

I would have a many to many relationship between ID and City. Many ID's can be registered to many cities?

 

But then i have no primary key in that table...

 

Can I have a table like this with two fields and no primary key? or would i need to create another column as a primary key?

 

P.S tried the SQL and got an mysql_fetch_array() error.

Ok,

 

Now I have something like this

 

Listing_ID        ID      City

1Manchester    1        Manchester

1Liverpool        1        Liverpool

2Manchester    2        Manchester

3London          3        London

 

I want to Count how many 'ID's are in each area and have the results display like.

 

Manchester  2

Liverpool      1

London        1

 

I'm more of a PHP/HTML'er so as you can guess im still learning SQL...learning and willing to learn :)

....Maybe i will make an autonumber.

 

Thanks for your help, its working great.

 

Though now ive changed the table setup I have a few other queries which need changing...

 

The variable $city is defined.

 

The following query selects member details from a certain area.

 

SELECT ID,Name,Price,Website,Contact,Profile FROM Members WHERE City='$city'

 

City was in "Members" and now it is in the table we have created above "Cities".

 

I take it I need to Join a query between the two tables?

Yeah.

Optimally you should have three tables. One for Members, one for Cities, and one that connects both (and has only two columns with ids).

 

Then it's like this.

 

SELECT
m.ID,m.Name,m.Price,m.Website,m.Contact,m.Profile 
FROM Members AS m
INNER JOIN Members_to_Cities AS m2c
  ON m.ID = m2c.memberID
INNER JOIN Cities AS c
  ON c.ID = m2c.cityID
WHERE
  c.City = '$city';

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.