tags:

views:

104

answers:

2

I m trying to get construct a sql statement where many to many relationship occurs. Basically i want to retrieve business based on 2 categories some business are included in both categories and queried through zip code. So far i got the zip code part but it doesnt distinguish the category and i don't have a table for the categories also what would be the most efficient way to do this?

here is sql statement so far

$query = "SELECT * FROM contacts WHERE zipcode in ( " . join(",", $zipcodes) . " ) ";

Here is the Table structures very simple. company_name char(55) NOT NULL,

`phone` char(20) default NULL,
`email` char(40) default NULL,
`address` text,
`city` text,
`state` text,
`zipcode` varchar(5) default NULL,
`id` varchar(10) NOT NULL,
`geo_id` varchar(20) NOT NULL,
`website` varchar(40) NOT NULL,
`vendor_id` varchar(10) NOT NULL

`id` int(11) unsigned NOT NULL auto_increment,
`zip_code` varchar(5) character set utf8 collate utf8_bin NOT NULL,
`city` varchar(50) character set utf8 collate utf8_bin default NULL,
`county` varchar(50) character set utf8 collate utf8_bin default NULL,
`state_name` varchar(50) character set utf8 collate utf8_bin default NULL,
`state_prefix` varchar(2) character set utf8 collate utf8_bin default NULL,
`area_code` varchar(3) character set utf8 collate utf8_bin default NULL,
`time_zone` varchar(50) character set utf8 collate utf8_bin default NULL,
`lat` float NOT NULL,
`lon` float NOT NULL,
PRIMARY KEY  (`id`),
KEY `zip_code` (`zip_code`)

I dont have the category table. I will make a table with 2 categories (retail and hospitality) only problem is how i can querry this table to determine if contact is in;

retail hospitality or both...

A: 

Not really sure, because of the rather low level of detail, but what about:

$query = "SELECT * FROM contacts WHERE zipcode in ( " . join(",", $zipcodes) . " ) AND category1 = YOUR_DESIRED_VALUE AND category2 = YOUR_DESIRED_VALUE;";

or if it only needs to be in one category:

$query = "SELECT * FROM contacts WHERE zipcode in ( " . join(",", $zipcodes) . " ) AND (category1 = YOUR_DESIRED_VALUE OR category2 = YOUR_DESIRED_VALUE;");
middus
how about if the business in only one category would this return an error
updated my answer accordingly
middus
A: 

Many-to-many relationships typically require a table just to store the relationship. I.e., not a contacts table, not a categories table, but a separate 'contact_categories' table.

The table itself can be very simple:

CREATE TABLE contact_categories (
  contact_id VARCHAR(10) NOT NULL REFERENCES contact (id),
  category_id INT NOT NULL
);

(With this table, you don't even need a separate categories table, if you can remember that category 1 is retail, and 2 is hospitality, for instance)

Then, if a contact is in one category, there is one row for it in the contact_categories table. If it is in two categories, then there are two rows, etc.

To get all of the businesses in category 1, then, your select looks like this:

SELECT contacts.*
FROM contacts
    JOIN contact_categories ON (contacts.id = contact_id);

If you want to restrict by zip code, just add the WHERE clause:

SELECT contacts.*
FROM contacts
    JOIN contact_categories ON (contacts.id = contact_id)
WHERE zipcode in ('12345','23456','90210');
Ian Clelland