views:

55

answers:

2

I need to query the database by joining two tables. Here is what I have:

Table Town:

  • id
  • name
  • region

Table Supplier:

  • id
  • name
  • town_id

I currently have the following query which outputs all the Towns that belong to a given region:

SELECT id, name FROM Town WHERE region = 'North West';

Now I need to extend this query and create two further queries as follows:

  • Output the number of Suppliers for each Town

  • Output only the Towns that have 1 or more Supplier

I am using PHP for my scripts if that helps. I know I may be able to to get this data using PHP but in terms of performance it will probably be better if it is done in MySQL.

EDIT (27/07/10):

I now needs to extend this one last time - there is another table called Supplier_vehicles:

  • id
  • supplier_id
  • vehicle_id

A Supplier can have many Supplier_vehicles. The count (NumSupplier in this case) needs to now contain the total number of suppliers in a given town that have any of the given vehicle_id (IN condition):

SELECT * FROM Supplier s, Supplier_vehicles v WHERE s.id = v.supplier_id AND v.vehicle_id IN (1, 4, 6)

Need to integrate the above query into the existing JOIN query.

+4  A: 

Count the number of suppliers.

SELECT t.id, t.name, count(s.id) as NumSupplier
FROM Town t
LEFT OUTER JOIN Suppliers s ON s.town_id = t.id
GROUP BY t.id, t.name

Only towns that have at least one supplier

SELECT DISTINCT t.id, t.name
FROM Town t
INNER JOIN Suppliers s ON s.town_id = t.id

And you are 100% correct, the best place for this is an SQL query.

Chris Diver
Hi Chris, on that first query I'm only getting back one row, and that's the very first record in my Town table...
GSTAR
That query should work fine, how are you executing it?
Chris Diver
It's working fine now mate, cheers.
GSTAR
On that second solution (Only towns that have at least one supplier) if I also wanted to output the number of suppliers I can add in the COUNT and GROUP BY functions as in the first solution. I found however that I can take out DISTINCT and it will still give me the same results. So does it actually make any difference if we have DISTINCT or not?
GSTAR
How can we extend the first query further to give a count of only the suppliers (within each town) that have field "Suppliers.enabled=1"? I tried adding this as a WHERE clause but it does not return all the rows in the table.
GSTAR
`GROUP BY` makes it `DISTINCT` because it will only display one row for each id/name with `GROUP BY`. `DISTINCT` only returns one of each row that is returned by the query. To make suppliers.enabled work, put it in the join so it becomes `LEFT OUTER JOIN Suppliers s ON s.town_id = t.id AND s.enabled = 1`
Chris Diver
Finally is it possible to retrieve the total count of the NumSupplier field?
GSTAR
If every supplier has a town then it is `SELECT COUNT(id) FROM Suppliers`
Chris Diver
No, I meant based on that first query...
GSTAR
If every supplier has a town then they are equivalent, or just remove the first two columns in the select and the group by.
Chris Diver
Hi Chris, can you take a look at my recent edit please? Thanks.
GSTAR
I'm not sure what you mean by intergrate into the first query. You should ask another question, giving an example of what you expect the output to be.
Chris Diver
Cheers, have posted a new question: http://stackoverflow.com/questions/3343138/mysql-join-query-possible-two-inner-joins
GSTAR
A: 
SELECT t.id, t.name, count(s.id) as NumSupplier
FROM Town t
LEFT JOIN Suppliers s
[WHERE NumSupplier > 1]
GROUP BY t.id
Sjoerd