使用内部联接实现联合所有查询
Implement a union all query with an inner join
我有 2 个 table 这样的:
// 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
与这些 table 一起使用以获得结果 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
时,您只是合并 table 中的行。因此,如果我 运行 以下查询:
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 示例,因此您可以直观地并排查看差异。
我有 2 个 table 这样的:
// 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
与这些 table 一起使用以获得结果 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
时,您只是合并 table 中的行。因此,如果我 运行 以下查询:
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 示例,因此您可以直观地并排查看差异。