简体   繁体   English

SQL语句多对多

[英]SQL Statement many-to-many

I'm trying to construct a SQL statement where many to many relationship occurs. 我正在尝试构造一条发生多对多关系的SQL语句。 Basically I want to retrieve business based on 2 categories some business are included in both categories and queried through zip code. 基本上,我想根据2个类别检索业务,两个类别中都包含一些业务,并通过邮政编码查询。 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: 到目前为止,这是SQL语句:

$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? 我将创建一个包含2类(零售和款待)的表,唯一的问题是如何查询此表以确定联系人是否在?

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. 即,不是联系人表,不是类别表,而是单独的“ contact_categories”表。

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) (例如,使用此表,您甚至不需要一个单独的类别表,例如,如果您记得类别1是零售,类别2是招待,那么)

Then, if a contact is in one category, there is one row for it in the contact_categories table. 然后,如果某个联系人属于一个类别,则contact_categories表中有一行。 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: 要获得类别1中的所有业务,您的选择如下所示:

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: 如果要通过邮政编码限制,只需添加WHERE子句:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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