0

我有一个数据库,里面有很多业务以及它们之间的关系。我正在尝试找到一种方法来让 B2 出售给 B1 的所有企业也不出售。但仅在 B1 和 B2 出售给同一企业的 B2 上。

V = 供应商,C = 客户,两者都是企业,但从供应商/客户的角度思考会使这更容易解释。

V1 - 卖给-> C1 <- 卖给- V2 - 卖给-> C2

我正在为特定的 V1 起点寻找所有不在 C1 中的 C2。

我目前的表:

mysql> DESCRIBE business;  
+---------------+-------------------+------+-----+----------+----------------+
| Field         | Type              | Null | Key | Default  | Extra          |
+---------------+-------------------+------+-----+----------+----------------+
| business_id   | int(11)           | NO   | PRI | NULL     | auto_increment |
| email         | varchar(255)      | YES  |     |          |                |
| name          | varchar(255)      | NO   |     | NULL     |                |
| city          | varchar(255)      | YES  |     |          |                |
| state         | varchar(255)      | YES  | MUL |          |                |
| cCount        | int(10) unsigned  | YES  | MUL | 0        |                |
| scCount       | int(10) unsigned  | YES  | MUL | 0        |                |
| vCount        | int(10) unsigned  | YES  | MUL | 0        |                |
| svCount       | int(10) unsigned  | YES  | MUL | 0        |                |
+---------------+-------------------+------+-----+----------+----------------+

mysql> DESCRIBE relation_sells_to;
+---------+----------+------+-----+---------+--------+
| Field   | Type     | Null | Key | Default | Extra  |
+---------+----------+------+-----+---------+--------+
| start   | int(11)  | NO   | MUL | NULL    |        |
| end     | int(11)  | NO   | MUL | NULL    |        |
+---------+----------+------+-----+---------+--------+

mysql> DESCRIBE vcvc;
+---------+---------+------+-----+---------+-------+
| Field   | Type    | Null | Key | Default | Extra |
+---------+---------+------+-----+---------+-------+
| vendor  | int(11) | NO   | MUL | NULL    |       |
| client  | int(11) | NO   |     | NULL    |       |
| vendor2 | int(11) | NO   |     | NULL    |       |
| client2 | int(11) | NO   |     | NULL    |       |
+---------+---------+------+-----+---------+-------+

我可以使用这两个查询来获得最新的结果,但是每次我想更新它时我都必须删除 vcvc 表,目前构建它需要 15 分钟。我也不需要在任何时候访问所有这些信息。我可能需要一次生成几千个这些列表,而不是完整的数据库。

CREATE TABLE IF NOT EXISTS vcvc (INDEX vendor (vendor), INDEX client2 (client2))
SELECT r.start AS vendor, r.end AS client, r2.start AS vendor2, r3.end AS client2
FROM relation_sells_to AS r
JOIN relation_sells_to as r2 ON r.end = r2.end
JOIN relation_sells_to AS r3 ON r2.start = r3.start
WHERE r.start != r2.start
AND r.end != r3.end;

SELECT DISTINCT(client2), cCount, scCount, vCount, svCount 
FROM vcvc 
JOIN business AS b ON client2=b.business_id 
WHERE vendor = #### 
AND client2 NOT IN (SELECT client FROM vcvc WHERE vendor = ####) 
ORDER BY cCount DESC;

有没有办法在单个查询中做到这一点,这样我每次需要获取这些信息时都不必构建一个全新的表?我目前有 50 万个关系,当我构建这个表时,我最终得到了超过 1 亿行,其中大部分是我不需要的。理想情况下,我可以将电子邮件(首选)或 business_id 作为起始业务/供应商,然后只返回具有名称、城市、州、cCount、vCount 的业务/客户列表,并让它们按 vCount 和 DESC 顺序排列可能限制20ish。速度不是一个大问题,我真的只是希望能够获得当前结果,而不必构建一个大表,我需要 100+ 百万行中的 20 行。我最初尝试使用临时表,但因为我无法像当前查询需要的那样“重新打开”临时表。

谢谢您的帮助。

使用示例数据和所需输出进行更新。某些 cCount 和 vCounts 可能已关闭,我复制了真实数据然后对其进行了修改,但我不知道我是否正确更新了所有数字,因为我添加了所需的连接以显示我需要的内容。

mysql> SELECT * FROM business;
+-------------+----------------+----------------------+----------------+------------+--------+---------+--------+---------+
| business_id | email          | name                 | city           | state      | cCount | scCount | vCount | svCount |
+-------------+----------------+----------------------+----------------+------------+--------+---------+--------+---------+
|        1    | bob@bob.com    | Bobs Construction    | Virginia Beach | Virginia   |     62 |       3 |      0 |       0 |
|        2    | sue@bob.com    | Upholstery by Sue    | Austin         | Texas      |     20 |       3 |      4 |       4 |
|        3    | jim@bob.com    | Jim & Associates     | Crowley        | Texas      |      5 |       3 |      0 |       0 |
|        4    | jon@bob.com    | Jon Jon architects   | Costa Mesa     | California |     67 |       3 |      0 |       0 |
|        5    | joe@bob.com    | Joes Pizza           | Hamden         | Conecticut |     7  |       1 |      0 |       0 |
|        6    | tim@bob.com    | Tims WIndows         | Miami          | Florida    |     10 |       2 |      0 |       0 |
|        7    | ron@bob.com    | Rons Hot Rods        | Costa Mesa     | California |     8  |       4 |      0 |       0 |
+-------------+----------------+----------------------+----------------+------------+--------+---------+--------+---------+

mysql> SELECT start, end FROM relation_sells_to;
+-------+--------+
| start | end    |
+-------+--------+
|  3    | 1      |
|  3    | 2      |
|  2    | 4      |
|  2    | 5      |
|  4    | 2      |
|  1    | 5      |
|  4    | 5      |
|  4    | 6      |
|  4    | 7      |
+-------+--------+

-- Run code to build vcvc table.  Code is above.

mysql> SELECT * FROM vcvc WHERE vendor = 3;
+--------+--------+---------+---------+
| vendor | client | vendor2 | client2 |
+--------+--------+---------+---------+
|   3    | 1      |  2      |    4    |
|   3    | 1      |  2      |    5    |
|   3    | 1      |  4      |    2    |
|   3    | 2      |  1      |    5    |
|   3    | 2      |  4      |    5    |
|   3    | 2      |  4      |    6    |
|   3    | 2      |  4      |    7    |
+--------+--------+---------+---------+

Desired Output:

Select client_id, name, city, state, cCount, vCount FROM MAGIC WHERE email = jim@bob.com
+-----------+---------------------+----------------+------------+--------+---------+
| client_id | name                | city           | state      | cCount | vCount  |
+-----------+---------------------+----------------+------------+--------+---------+
|   4       | Jon Jon architects  | Costa Mesa     | California |     67 |       0 |
|   6       | Tims WIndows        | Miami          | Florida    |     10 |       0 |
|   7       | Rons Hot Rods       | Costa Mesa     | California |     8  |       0 |
|   5       | Joes Pizza          | Hamden         | Conecticut |     7  |       0 |
+-----------+---------------------+----------------+------------+--------+---------+
-- Ordered by cCount, business_id 2 is NOT in this list because 3 sells to 2 so I don't care that 4 sells to 2.

更新 2:

http://sqlfiddle.com/#!9/fdcec/2

这是它的一个小提琴。小提琴中的数据与上面的数据略有不同,因为它有 2-3 个以上的连接来正确显示我想要的内容。

4

0 回答 0