【发布时间】:2015-08-01 20:13:00
【问题描述】:
我有 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 等于“测试”时,需要结果:
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 |
+-----+------------------------------------------------------------------------------------------+
现在我可以使用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
【问题讨论】:
-
如果您需要始终将它们视为“假单曲”,将这些表格组合成一个表格不是更容易吗?
-
@MarcB 什么???当我使用
inner join而不是union all时,搜索速度会提高! -
为什么我要问,让
single_table拥有两个表中的数据,而不是两个单独的表,你需要将它们与查询结合起来,这不是更聪明吗? -
8 行 ?连接自然会将行彼此相邻,您应该有 4 行和 4 列
-
@MarcB 啊哈,这是一个例子,事实上我有 5 个表,每个表都有超过 300,000 行。我无法创建一张表。