内部加入一个表多次

时间:2013-01-14 14:47:51

标签: mysql inner-join

我正在尝试从几个不同的表创建派生表,如果我执行单个内部联接,一切都有效,但是当我添加更多联接时,我无法获得所需的结果。这是我现在得到的:

表格结构:

第一张表:

+------------+------------+------+-----+---------+-------+
| Field      | Type       | Null | Key | Default | Extra |
+------------+------------+------+-----+---------+-------+
| TAG_ID     | int(11)    | NO   | PRI | NULL    |       |
| STATUS     | int(11)    | YES  |     | NULL    |       |
| ENABLE     | int(11)    | YES  |     | NULL    |       |
| TIME_STAMP | bigint(20) | YES  |     | NULL    |       |
| VALUE      | float      | YES  |     | NULL    |       |
+------------+------------+------+-----+---------+-------+

第二张表:

+--------------------+---------------------+------+-----+------------+----------------+
| Field              | Type                | Null | Key | Default    | Extra          |
+--------------------+---------------------+------+-----+------------+----------------+
| TAG_ID             | int(11)             | NO   | PRI | NULL       | auto_increment |
| TAG_DEV_ID         | int(11)             | NO   |     | NULL       |                |
| TAG_NAME           | varchar(256)        | NO   |     | NA         |                |

.....................

| TAG_TIME_STAMP     | bigint(20) unsigned | NO   |     | 0          |                |
+--------------------+---------------------+------+-----+------------+----------------+

以下是我现在所做的事情 - 首先,我将它们加入一个大表后创建两个表的视图:

CREATE VIEW U1_TEMP_TEST AS (SELECT TAG_ID AS TID, VALUE FROM table_1);
CREATE VIEW U1_TEST AS (SELECT * FROM table_2 AS T2 INNER JOIN U1_TEMP_TEST AS T1 ON T2.TAG_ID = T1.TID);

现在我需要的是将表连接到自身,这样我就可以在不同的COLUMNS中从'VALUE'获取行,我这样做:

SELECT U1.TAG_DEV_ID, U1.TAG_ID, U1.VALUE, U2.VALUE 
FROM U1_TEST AS U1 
INNER JOIN ((SELECT * FROM U1_TEST 
        WHERE (TAG_ID = 1 OR TAG_ID = 12 OR TAG_ID = 21)) AS U2) 
        ON U1.TAG_DEV_ID = U2.TAG_DEV_ID 
WHERE (U1.TAG_ID = 4 OR U1.TAG_ID = 20 OR U1.TAG_ID = 14) 
GROUP BY U1.TAG_DEV_ID;

我得到了很好的结果:

+------------+--------+--------+-------+
| TAG_DEV_ID | TAG_ID |  VALUE | VALUE |
+------------+--------+--------+-------+
|          1 |      4 |   -0.5 |   2.1 |
|          2 |     14 |  -12.7 |   0.4 |
|          3 |     20 |   -5.7 |   9.5 |
+------------+--------+--------+-------+

但是,如果我尝试再添加一个这样的连接:

SELECT U1.TAG_DEV_ID, U1.TAG_ID, U1.VALUE, U2.VALUE, U3.VALUE 
FROM U1_TEST AS U1 
INNER JOIN ((SELECT * FROM U1_TEST 
    WHERE (TAG_ID = 1 OR TAG_ID = 12 OR TAG_ID = 21)) AS U2) 
    ON U1.TAG_DEV_ID = U2.TAG_DEV_ID 
INNER JOIN ((SELECT * FROM U1_TEST 
    WHERE (TAG_ID = 3 OR TAG_ID = 13 OR TAG_ID = 22)) AS U3) 
    ON U1.TAG_DEV_ID = U3.TAG_DEV_ID 
WHERE U1.TAG_ID = 4 OR U1.TAG_ID = 14 OR U1.TAG_ID = 24 
GROUP BY U1.TAG_DEV_ID;

返回的数据不是我所期望的,并非所有值都被选中。 也许第二次内连接有问题,我坚持了几个小时,无法理解为什么单个连接查询工作正常而最后一个没有。

感谢您的帮助!

编辑结果数据:

+------------+--------+-------+-------+-------+
| TAG_DEV_ID | TAG_ID | VALUE | VALUE | VALUE |
+------------+--------+-------+-------+-------+
|          1 |      4 |  -0.5 |     3 |     0 |
|          2 |     14 | -12.7 |   0.5 |   1.6 |
+------------+--------+-------+-------+-------+

我希望它是这样的:

+------------+--------+-------+-------+-------+
| TAG_DEV_ID | TAG_ID | VALUE | VALUE | VALUE |
+------------+--------+-------+-------+-------+
|          1 |      4 |  -0.5 |     3 |     0 |
|          2 |     14 | -12.7 |   0.5 |   1.6 |
|          3 |     21 |     x |     x |     x |
+------------+--------+-------+-------+-------+

2 个答案:

答案 0 :(得分:0)

像这样使用LEFT JOIN

SELECT 
  U1.TAG_DEV_ID, 
  U1.TAG_ID, 
  U1.VALUE, 
  U2.VALUE, 
  U3.VALUE 
FROM U1_TEST AS U1 
LEFT JOIN U1_TEST AS U2  ON U1.TAG_DEV_ID = U2.TAG_DEV_ID 
                        AND U3.TAG_ID IN(1, 12, 21) 
LEFT JOIN U1_TEST AS U3  ON U1.TAG_DEV_ID = U3.TAG_DEV_ID 
                        AND U3.TAG_ID IN(3, 13, 22) 
WHERE U1.TAG_ID IN(4, 14, 24)
GROUP BY U1.TAG_DEV_ID;

答案 1 :(得分:0)

您正在以错误的方式使用联接。您的where子句仅过滤3个表中的一个。您的查询如下所示:

SELECT U1.TAG_DEV_ID, U1.TAG_ID, U1.VALUE, U2.VALUE, U3.VALUE 
FROM U1_TEST AS U1 
INNER JOIN ((SELECT * FROM U1_TEST 
    WHERE (TAG_ID = 1 OR TAG_ID = 12 OR TAG_ID = 21)) AS U2) 
    ON U1.TAG_DEV_ID = U2.TAG_DEV_ID 
INNER JOIN ((SELECT * FROM U1_TEST 
    WHERE (TAG_ID = 3 OR TAG_ID = 13 OR TAG_ID = 22)) AS U3) 
    ON U1.TAG_DEV_ID = U3.TAG_DEV_ID 
WHERE U1.TAG_ID = 4 OR U1.TAG_ID = 14 OR U1.TAG_ID = 24 
GROUP BY U1.TAG_DEV_ID;

问题:

1。)WHERE (TAG_ID = 1 OR TAG_ID = 12 OR TAG_ID = 21)) AS U2)仅适用于U2

2。)WHERE (TAG_ID = 3 OR TAG_ID = 13 OR TAG_ID = 22)) AS U3)仅适用于U3

因此,根据定义,您预期的Tag_ID = 21的行不能在您的结果中,因为它是U1和U2的一部分,但您在U3的过滤器排除了记录,并且U3中没有任何内容可以加入Tag_ID = 21。

相反,我会使用如下的查询:

SELECT U1.TAG_DEV_ID, U1.TAG_ID, U1.VALUE, U2.VALUE, U3.VALUE 
FROM U1_TEST AS U1 
INNER JOIN TEST AS U2 
ON U1.TAG_DEV_ID = U2.TAG_DEV_ID 
INNER JOIN U3
    ON U1.TAG_DEV_ID = U3.TAG_DEV_ID 
    WHERE (TAG_ID = 1 OR TAG_ID = 12 OR TAG_ID = 21)) AND 
    (TAG_ID = 3 OR TAG_ID = 13 OR TAG_ID = 22)) AND
    U1.TAG_ID = 4 OR U1.TAG_ID = 14 OR U1.TAG_ID = 24 
GROUP BY U1.TAG_DEV_ID;

我没有测试这段代码,但是你明白了。