使用内部联接实现union all查询

时间:2015-05-20 14:05:40

标签: mysql sql join union

我有2张这样的表:

//             table1
+-----------------+-----------------+
|       col1      |        id       |
|-----------------+-----------------|
+-----------------+-----------------+
|       test      |        1        |
|-----------------+-----------------|
|       test      |        2        |
|-----------------+-----------------|
|    anything     |        3        |
|-----------------+-----------------|
|    anything     |        4        |
|-----------------+-----------------|


//             table2
+-----------------+-----------------+
|       col1      |        id       |
|-----------------+-----------------|
+-----------------+-----------------+
|       test      |        5        |
|-----------------+-----------------|
|       test      |        6        |
|-----------------+-----------------|
|    anything     |        7        |
|-----------------+-----------------|
|    anything     |        8        |
|-----------------+-----------------|

当我使用union all获取id col1等于'test'的值时,需要结果:

select * from table1 where col1='test'
union all
select * from table2 where col1='test'

// the result of this code is: 4 rows. id{1,2,5,6}

然后,为了更快更好的性能,我使用inner join实现了它,但不希望得到结果:

select * from table1 t1 inner join table2 t2
on t1.col1=t2.col1
where t1.col1='test'

// the result of this code is: 8 rows. id{1-5,1-6,2-5,2-6}

如何在这些表中使用inner join来获取结果ID {1,2,5,6}?

修改

示例:

table1 {[col1]=word, [col2]=mean}
+-----+------------------------------------------------------------------------------------------+
|  a  | used when referring to someone or something for the first time in a text or conversation |
|-----|------------------------------------------------------------------------------------------|
|  a  | used to indicate membership of a class of people or things                               |
|-----|------------------------------------------------------------------------------------------|
|  x  | xxxxx                                                                                    |
+-----+------------------------------------------------------------------------------------------+

table2 {[col1]=word, [col2]=mean}
+-----+------------------------------------------------------------------------------------------+
|  a  | the blood group whose red cells carry the A antigen                                      |
|-----|------------------------------------------------------------------------------------------|
|  x  | xxxxx                                                                                    |
+-----+------------------------------------------------------------------------------------------+

现在可以使用joinecho吗? :

a | used when referring to someone or something for the first time in a text or conversation
a | used to indicate membership of a class of people or things
a | the blood group whose red cells carry the A antigen 

1 个答案:

答案 0 :(得分:3)

使用内部联接,您无法轻松完成此操作。考虑一下内连接的作用,它根据相关列将它们放在一起。例如,如果您运行以下查询:

SELECT *
FROM table1
JOIN table2 ON table2.col1 = table1.col1 AND table2.col1 = 'test';

你会看到这样的结果:

| col1 | id | col1 | id |
+------+----+------+----+
| test | 1  | test | 5  |
| test | 2  | test | 5  |
| test | 1  | test | 6  |
| test | 2  | test | 6  |

此时,您可能会尝试对两列中的每一列中的不同值运行查询,但据我所知,这是不可能的。

因此,我不相信您可以使用UNION ALL或任何联接替换INNER JOIN查询。即使您执行了交叉联接,您也只能在自己的列中获得table1.id,并在单独的列中获得table2.id,这会产生与上述相同的问题。

修改

当您使用union all时,您只需合并表格中的行。因此,如果我运行以下查询:

SELECT col1, id
FROM table1
WHERE col1 = 'test'
UNION ALL
SELECT col1, id
FROM table2
WHERE col1 = 'test'

你会看到:

| col1 | id |
+------+----+
| test | 1  |
| test | 2  |
| test | 5  |
| test | 6  |

因为它从两个单独的查询中获取结果集并将它们组合在一起。这是一个显示两个查询的SQL Fiddle示例,因此您可以直观地看到差异。