简体   繁体   中英

SQL Statement many-to-many

I'm trying to 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 doesn't 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 the 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 don't have the category table. I will make a table with 2 categories (retail and hospitality) only problem is how I can query this table to determine if contact is in?

retail hospitality or both...

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;");

Many-to-many relationships typically require a table just to store the relationship. Ie, 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');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM