Sunday, March 30, 2008

Two tables, count records and filter by x or y (2 replies)

Hello!

I'm working on a big project but I can't get any help. I would be very happy if someone could help me out.

I have two tables called onkormanyzat and cko. They both have an ID column called ok_id. They also have a telepules (town) and a bemutato_resztv (participated) column which can be 'Igen' or 'Nem'. I would like to get all the telepules (towns) from both cko and onkormanyzat and count the ones which have 'Igen' in participated (bemutato_resztv). But i don't want to have duplicate towns (telepules). It is poosible that only one table has that town with Igen and the other doesnt. So if any of the tables with the selected telepules has 'Igen' it counts.

$query = "SELECT cko.megye,onkormanyzat.ok_id,
COUNT(cko.ok_id) FROM onkormanyzat JOIN cko ON
(onkormanyzat.telepules = cko.telepules)
AND cko.bemutato_resztv='Igen'
GROUP BY cko.megye";

I tried this one, but it only counts the telepules or ok_id (at this time) if both the telepules is in both tables and if cko has bemutato_reszt='Igen'.

COUNT telepules from both tables (cko,onkormanyzat) when any of them has bemutato_resztv='Igen' without duplicate telepules.



Thank You!

Benedek Rakovics
CoreSTAT Consulting Bt.
Skype: corestat.consulting