mysql - 使用内部联接实现联合所有查询

标签 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}

然后,为了更快更好的性能,我使用内部连接实现了它,但结果并不理想:

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}

如何对这些表使用内部联接来获取结果 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 

最佳答案

您无法使用内部联接轻易地做到这一点。想一想内部联接的作用,它根据相关列将它们放置在彼此相邻的位置。例如,如果您运行以下查询:

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显示两个查询的示例,以便您可以直观地并排查看差异。

关于mysql - 使用内部联接实现联合所有查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30352073/

相关文章:

sql - 如何编写查询显示表 A 中的记录而表 B 中没有匹配的记录?

join - 如何将pandas数据帧的每一行附加到另一个数据帧的每一行

java - 使用 Atmosphere 进行实时协作编辑

mysql - 从列名为 "name_en-GB"的数据库中检索数据

javascript - 使用 javascript 参数选择查询

使用分层索引将 Pandas 系列连接回源 DataFrame

sql - join 子句与 where 子句中的 oracle sql 自连接过滤器

php - 如何在 MySQL 中显示重复记录并使用 PHP 合并记录?

mysql - 当我在非唯一列中设置外键关系时,GROUP BY 不起作用

sql - 替换列值