MySql:由多个连接返回的差异两列

时间:2015-03-02 06:55:37

标签: mysql sql many-to-many query-performance set-difference

我有一个拥有大量业务及其关系的数据库。我试图找到一种方法来获得B2销售给B1的所有商家也不卖。但只有在B1和B2销售给同一业务的B2上。

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

V1 -sells to-> 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;

有没有办法在单个查询中执行此操作,因此每次需要获取此信息时,我都不必构建一个全新的表?我目前有500k的关系,当我建立这个表时,我最终有超过1亿行和大多数我不需要的行。理想情况下,我可以将电子邮件(首选)或business_id作为起始业务/供应商,然后只返回名称,城市,州,cCount,vCount的业务/客户列表,并按vCount顺序以DESC顺序进行订购。可能是LIMIT 20ish。速度不是一个巨大的问题我真的只是希望能够获得当前的结果,而不必构建一个大表,其中我需要20个100 +百万行。我最初尝试使用临时表,但因为我不能重新打开#34;临时表就像我当前的查询需要一样。

感谢您的帮助。

使用样本数据和所需输出进行更新。 有些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个连接来正确显示我想要的内容。

0 个答案:

没有答案