我有 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 |
+-----+------------------------------------------------------------------------------------------+
现在我可以使用 join
和 echo
吗? :
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/